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 set 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:

    path('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 classes to be applied
to form class attribute. The form will always have by default ‘uniForm’ class.
form_group_wrapper_class: String containing separated CSS classes to be applied
to each row of inputs.
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”.
include_media: Whether to automatically include form media. Set to False if
you want to manually include form media outside the form. Defaults to True.

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(template_pack=<SimpleLazyObject: 'bootstrap'>)[source]

Used by crispy_forms_tags to get helper attributes

render_layout(form, context, template_pack=<SimpleLazyObject: 'bootstrap'>)[source]

Returns safe html of the rendering of the layout