Crispy form: how to disable/readonyl all fields?

679 views
Skip to first unread message

Paul

unread,
May 6, 2012, 1:56:32 PM5/6/12
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?

I'm half way there:
- I created a form class that has the FornHelper
- 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'

This all works but the only thing left is to disable/readonly all the
fields... how can i do that?

Thanks for any idea's
Paul

Paul

unread,
May 7, 2012, 2:32:57 PM5/7/12
to Django users
I have a sort of solution (but don't like it). I subclassed the crispy-
tag to change the input's into span's/uneditable's. This is done using
re's which is the part i don't like, it will only work as long as the
raw html is compatible with the re's....

I know a better solution is to create custom templates for the field
for example but i'm missing complete and working examples to do that;
anyone advise?

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)
Reply all
Reply to author
Forward
0 new messages