How it works...

As a result, we get the following form:

Contrary to the default behavior of Django, which hardcodes field generation in Python code, the django-crispy-forms app uses templates to render the fields. You can browse them under crispy_forms/templates/bootstrap3, and copy some of them to an analogous path in your project's template directory to overwrite them when necessary.

In our movie creation form, we pass a custom template for the categories field that will add the .level-* CSS classes to the <label> tag, wrapping the checkboxes. One problem with the normal CheckboxSelectMultiple widget is that when rendered it only uses choice values and choice texts, whereas we need other properties of the category such as the depth level. To solve this, we also created a custom MultipleChoiceTreeField form field, which extends ModelMultipleChoiceField and overrides the label_from_instance method to return the category instance itself, instead of its Unicode representation. The template for the field looks complicated; however, it is mostly a combination of a common field template (crispy_forms/templates/bootstrap3/field.html) and multiple checkbox field template (crispy_forms/templates/bootstrap3/layout/checkboxselectmultiple.html), with all the necessary Bootstrap markup. We just made a slight modification to add the .level-* CSS classes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.70.93