在标签前显示 BooleanField 复选框
Show BooleanField checkbox before label
我正在努力尽量减少渲染具有 bootstrap 样式的表单所需的代码量,希望仅使用 {{ form }}
进行渲染,但我还没有尚未设法找到一种方法来呈现带有文本前复选框的 BooleanField
。
from django.forms import Form, BooleanField
class MyForm(Form):
field = BooleanField(label='Test Label')
MyForm().as_table()
以上测试代码会输出
<tr><th><label for="id_field">Test Label:</label></th><td><input class="" id="id_field" name="field" type="checkbox" /></td></tr>
但我希望实现的外观和感觉与 bootstrap docs 中所示的相同。
<label for="id_field"><input class="" id="id_field" name="field" type="checkbox" />Test Label:</label>
这样做的问题是渲染是通过表单处理的,其中 label and the field 是 positioned/rendered 分开的,我还没有找到可以覆盖的地方在标签内渲染小部件...
有什么办法可以实现吗?
我不想使用 django-bootstrap3 等,我也查看了它们的源代码,但也看不到他们设法实现此目的的任何地方。
事实证明,这比看起来要复杂得多,涉及提供表单字段混合以及自定义小部件。
我要维护的工作太多了。
但是!
django-angular
has managed to achieve this with their own CheckboxInput
and associated BooleanFieldMixin
,因为这是我打算使用的东西,它解决了我的问题。
我正在努力尽量减少渲染具有 bootstrap 样式的表单所需的代码量,希望仅使用 {{ form }}
进行渲染,但我还没有尚未设法找到一种方法来呈现带有文本前复选框的 BooleanField
。
from django.forms import Form, BooleanField
class MyForm(Form):
field = BooleanField(label='Test Label')
MyForm().as_table()
以上测试代码会输出
<tr><th><label for="id_field">Test Label:</label></th><td><input class="" id="id_field" name="field" type="checkbox" /></td></tr>
但我希望实现的外观和感觉与 bootstrap docs 中所示的相同。
<label for="id_field"><input class="" id="id_field" name="field" type="checkbox" />Test Label:</label>
这样做的问题是渲染是通过表单处理的,其中 label and the field 是 positioned/rendered 分开的,我还没有找到可以覆盖的地方在标签内渲染小部件...
有什么办法可以实现吗?
我不想使用 django-bootstrap3 等,我也查看了它们的源代码,但也看不到他们设法实现此目的的任何地方。
事实证明,这比看起来要复杂得多,涉及提供表单字段混合以及自定义小部件。
我要维护的工作太多了。
但是!
django-angular
has managed to achieve this with their own CheckboxInput
and associated BooleanFieldMixin
,因为这是我打算使用的东西,它解决了我的问题。