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:
- Created 11 years ago
- Comments:14 (5 by maintainers)
And here I am again, 2 years later. Thanks past self!
I just battled with this for a bit and ended up using (ref):