Miguel Araujo: Django crispy-forms

published May 11, 2012

Miguel Araujo talks about Django crispy-forms at PyGrunn.

See the PyGrunn website for more info about this one-day Python conference in Groningen, The Netherlands.

In Django forms you can display a form in html in different ways. But it can be hard to customize or to target it using css. You can reorder fields using keyOrder, as fields is a SortedDict. ModelForms are different. You could override templates, but it gets hacky fast.

django-crispy-forms was formerly known as django-uni-forms, created by Daniel Greenfeld. I joined the project in 2010 and became the lead developer.

It has a filter |crispy and a tag {% crispy %}. It adds divs and ids to your form.

With the FormHelper class you control the global form rendering behaviour, using attributes, for example form_method='post', form_action, form_id, form_class, form_tag=False. You can define your own attributes, with the code behind it, to for example have help text as placeholder.

The layout power comes from layout objects. They are Python classes. Every layout object has an associated class. For example, Div, Fieldset, HTML. Div(Div('name', 'value'), HTML("

hello

")

Layouts can be decoupled, and composed into another Layout.

crispy-forms templates can be easily overridden at various levels. There are layout templates and global templates.

There is more power available. Documentation is at http://django-crispy-forms.rtfd.org