Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django crispy forms work with custom widgets?

Following the code in this post, I was able to group the options in a ModelMultipleChoiceField based on another field. As described in this post, I used a custom widget (literally the one that is in the post):

Grouping CheckboxSelectMultiple Options in Django

so now the form is rendering the way I'd like it to, but I was hoping to improve on the formatting by using crispy forms. however, when I added {% load crispy_forms_tags %} and {{form|crispy}} it rendered the form without the grouping that I worked so hard to achieve...

does anyone know how if its possible to apply crispy forms to a form with a custom widget and preserve what the widget does?

thanks!

like image 950
Yin Li Avatar asked Sep 05 '13 06:09

Yin Li


People also ask

Why use Django crispy forms?

Django-crispy-forms is an application that helps to manage Django forms. It allows adjusting forms' properties (such as method, send button or CSS classes) on the backend without having to re-write them in the template.

What is crispy form tags in Django?

django-crispy-forms provides you with a |crispy filter and {% crispy %} tag that will let you control the rendering behavior of your Django forms in a very elegant and DRY way. Have full control without writing custom form templates.


1 Answers

No, this won't be possible because crispy forms never calls the render method of the widget.

An alternative is to override the template for the widget, in this case bootstrap/layout/checkboxselectmultiple.html, to use the grouping as required.

https://github.com/maraujop/django-crispy-forms/blob/dev/crispy_forms/templates/bootstrap/layout/checkboxselectmultiple.html

like image 68
tuxcanfly Avatar answered Oct 30 '22 19:10

tuxcanfly