form set field django code example

Example 1: django set form field value in template

This was a feature request that got fixed in Django 1.3.

Here's the bug: https://code.djangoproject.com/ticket/10427

Basically, if you're running something after 1.3, in Django templates you can do:

{{ form.field.value|default_if_none:"" }}
Or in Jinja2:

{{ form.field.value()|default("") }}
Note that field.value() is a method, but in Django templates ()'s are omitted, while in Jinja2 method calls are explicit.

If you want to know what version of Django you're running, it will tell you when you do the runserver command.

If you are on something prior to 1.3, you can probably use the fix posted in the above bug: https://code.djangoproject.com/ticket/10427#comment:24

Example 2: django form field add attrs

widgets = {
  'field_name': django_forms.NumberInput(attrs={'step': 0.01}),
}