To get some more control over my input fields, I try to "copy" the function that generates input fields in the template
I use a ModelForm and I want to change
...
{{ form.name }}
...
into something like
...
<input id="{{ form.name.auto_id }}" type="text" name="{{ form.name.name }}" maxlength="{{ form.name.**get_max_length** }}" value="{{ form.name.**get_value_or_initial_value** }}"/>
...
Most of that works already, except maxlength and value.
maxlength
I read
- How can I programmatically obtain the max_length of a Django model field?
- Django: model maxlength and form maxlength
but I still don't get it right...
value
The answer to this question (How can you manually render a form field with its initial value set?) points to a ticket which is fixed already. Still name.value
would print "None" if there is no value present. Am I meant to catch value="None"
manually or is there a better solution meanwhile?
[edit]
value seems to work like that
{{ form.name.value|default_if_none:"" }}