Django crispy表单可与自定义小部件一起使用吗? [英] Django crispy forms work with custom widgets?

查看:52
本文介绍了Django crispy表单可与自定义小部件一起使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照本文中的代码,我能够基于另一个字段将选项组合在ModelMultipleChoiceField中.如本文所述,我使用了一个自定义小部件(实际上是该文章中的小部件):

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

在Django中对CheckboxSelectMultiple选项进行分组

因此,现在该表单正在呈现我想要的方式,但是我希望通过使用酥脆的表单来改善格式.但是,当我添加{%load crispy_forms_tags%}和{{form | crispy}}时,它呈现的表单没有我努力实现的分组...

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?

谢谢!

推荐答案

不,这是不可能的,因为酥脆的表格永远不会调用小部件的 render 方法.

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

一种替代方法是覆盖窗口小部件的模板(在本例中为 bootstrap/layout/checkboxselectmultiple.html ),以根据需要使用分组.

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

这篇关于Django crispy表单可与自定义小部件一起使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆