question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to disable/readonly all fields?

See original GitHub issue

(notice i have posted the same question to django.users)

I have a model that i would like to create/read/update and delete (CRUD); i’m using generic views (create_update) to do that. Also i’m using crispy forms to do the form (layout) styling icw twitter- bootstrap styling. Now the thing i’m not sure about is how to do the ‘read’ function. I prefer to show a similar page as the update function but then without a button and with all fields set to readonly mode (or disabled). How could i do that with crispy forms?

The solution i have now:

  • I created a form class that has the FormHelper
  • From the urlconf i first route to my own view function to retrieve the object (to read)
  • Then i call object_detail (from generic views) with extra_context that has the form initialized with the object to read: extra_context = {‘form’: ReadForm(instance = object)},
  • Then in the template i simply invoke crispy forms to display the ‘form’
  • to replace the <input …> with <span class-“uneditable-input”>…</span> i subclassed the crispy-tag and then post-process the html using re’s. Offcourse this will only work as long as the raw html is compatible with the re’s… which is the reason i don’t like it.

A better solution is probably to create custom templates for the field but i’m missing complete and working examples to do that. And also since the crispy (field) template invoke tags again that render other templates (and so on…) it’s difficult to reach the same quality level.

The BEST approach would offcourse be to have support from the crispy-forms module, using an attribute of some kind.

Paul

class CrispyUneditable(UniFormNode): ‘’‘subclass the crispy tag to modify the tags output: each input field shall be uneditable using bootstraps uneditable styling’‘’ def render(self, context): import re value = super(CrispyUneditable, self).render(context) return re.sub( r’<input (.?) value=“([^”]?)" class=“([^”])" (.?)>‘, r’<span class="\3 uneditable-input">\2</span>', value)

{% crispy-uneditable %} tag

@register.tag(name=“crispy-uneditable”) def do_crispy_uneditable_form(parser, token): token = token.split_contents() form = token.pop(1)

try: 
    helper = token.pop(1) 
except IndexError: 
    helper = None 


return CrispyUneditable(form, helper)

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
andytwoodscommented, Jun 12, 2020

And here I am again, 2 years later. Thanks past self!

2reactions
andytwoodscommented, Jun 21, 2018

I just battled with this for a bit and ended up using (ref):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        for nam, field in self.fields.items():
            field.disabled = True
Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I make an entire HTML form "readonly"?
Wrap the input fields and other stuff into a <fieldset> and give it the disabled="disabled" attribute. Example (http://jsfiddle.net/7qGHN/):.
Read more >
How do I make a field readonly or disabled so that it cannot be ...
Hello @kartik,. Setting readonly on a widget only makes the input in the browser read-only. Adding a clean_sku which returns instance.sku ...
Read more >
Disable (Read Only) All Input Field on Page Load using jQuery
jQuery to disable all input text fields along with textarea field using jQuery on page load, in this example we will learn to...
Read more >
Best way to disable all fields at form level and l... - ServiceNow
Hi Sri,. You can use UI policies to make fields read only at form and you can use ACLs by writing ACL rule...
Read more >
Dynamics CRM Read Only Form and Disable Fields with ...
On the Form Properties page, click Add under Event Handlers. Select the new library and for the function type ReadOnly: Click OK, Save...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found