API helpers

class helper.FormHelper(form=None)[source]

This class controls the form rendering behavior of the form passed to the {% crispy %} tag. For doing so you will need to set its attributes and pass the corresponding helper object to the tag:

{% crispy form form.helper %}

Let’s see what attributes you can set and what form behaviors they apply to:

form_method: Specifies form method attribute.
You can see it to ‘POST’ or ‘GET’. Defaults to ‘POST’
form_action: Applied to the form action attribute:
  • Can be a named url in your URLconf that can be executed via the {% url %} template tag. Example: ‘show_my_profile’. In your URLconf you could have something like:

    url(r'^show/profile/$', 'show_my_profile_view', name = 'show_my_profile')
    
  • It can simply point to a URL ‘/whatever/blabla/’.

form_id: Generates a form id for dom identification.
If no id provided then no id attribute is created on the form.
form_class: String containing separated CSS clases to be applied
to form class attribute. The form will always have by default ‘uniForm’ class.
form_tag: It specifies if <form></form> tags should be rendered when using a Layout.
If set to False it renders the form without the <form></form> tags. Defaults to True.
form_error_title: If a form has non_field_errors to display, they
are rendered in a div. You can set title’s div with this attribute. Example: “Oooops!” or “Form Errors”
formset_error_title: If a formset has non_form_errors to display, they
are rendered in a div. You can set title’s div with this attribute.
form_style: Uni-form has two built in different form styles. You can choose
your favorite. This can be set to “default” or “inline”. Defaults to “default”.

Public Methods:

add_input(input): You can add input buttons using this method. Inputs
added using this method will be rendered at the end of the form/formset.
add_layout(layout): You can add a Layout object to FormHelper. The Layout
specifies in a simple, clean and DRY way how the form fields should be rendered. You can wrap fields, order them, customize pretty much anything in the form.

Best way to add a helper to a form is adding a property named helper to the form that returns customized FormHelper object:

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit

class MyForm(forms.Form):
    title = forms.CharField(_("Title"))

    @property
    def helper(self):
        helper = FormHelper()
        helper.form_id = 'this-form-rocks'
        helper.form_class = 'search'
        helper.add_input(Submit('save', 'save'))
        [...]
        return helper

You can use it in a template doing:

{% load crispy_forms_tags %}
{% crispy form %}
get_attributes()[source]

Used by crispy_forms_tags to get helper attributes

render_layout(form, context)[source]

Returns safe html of the rendering of the layout

exception helper.FormHelpersException[source]

This is raised when building a form via helpers throws an error. We want to catch form helper errors as soon as possible because debugging templatetags is never fun.

crispy-forms

django-crispy-forms is a Django application that lets you easily build, customize and reuse forms using your favorite CSS framework, without writing template code and without having to take care of annoying details. You are currently looking at the documentation of the development release.

Support

If you love django-crispy-forms, consider making a small donation on Flattr:

Related Topics

This Page