Is there any clever way to make django forms render field with asterisks after fields that are required? Or to provide some other clever for to mark required fields? I wouldn't like to have to do it again in a template if I already set a field as required in the form.
A:
The best way for such purposes I have found is to render form's output via an html template. How to do this is described here. By luck, the example puts an asterisk after required fields just like you want.
shanyu
2009-08-10 10:15:00
None of my forms is rendered {{ form }}, I explicitly call fields, because I usually display only subset of form's fields. I need something lower, like an attribute of a field or a widget.
gruszczy
2009-08-10 10:21:09
If you are talking about modelforms and using 'fields' property of the Meta class to determine a subset of fields to display, this will work. If you have non-model forms and have hidden fields, again it will work. I am in a haste, sorry if I am missing something?
shanyu
2009-08-10 10:29:38
I have a single Form that is used in many templates and I usually call all the fields I need in the template. That's why I can't use templated rendering.But it seems it would be better, If I just created some subclasses with Meta and then use this templating. That's good idea, I have to think about it.
gruszczy
2009-08-10 10:49:30
A:
I use a template tag to render form fields with their labels and errors, plus an asterisk and a CSS class for required fields. There are various snippets available to do it on www.djangosnippets.org
Daniel Roseman
2009-08-10 12:53:29
So do I at the moment, I mean I use a filter to do this. Still, this isn't very dry. I'd prefer to put this setting into the form (some 'mark_required=True' in form).
gruszczy
2009-08-10 12:57:28