在标签之前显示BooleanField复选框 [英] Show BooleanField checkbox before label
问题描述
我正在尝试最大限度地减少使用代码渲染带有引导样式的表单,希望仅使用 {{form}}
但我还没有设法找到一个方法来渲染一个 BooleanField
,并在文本前面的复选框。
$ _ code from django.forms import Form,BooleanField
class MyForm(Form):
field = BooleanField(label ='Test Label')
MyForm ().as_table()
上述测试代码将输出
< tr>< th>< label for =id_field>测试标签:< / label>< / th>< td>< input class =id =id_fieldname =fieldtype =checkbox/>< / td>< / tr>
但是我希望达到的是一样的外观和感觉,如 bootstrap docs 。
这样做的问题是渲染通过窗体处理,其中标签和字段分开定位/渲染,我有没有找到一个覆盖的地方,这将允许我在标签内部渲染小部件...
任何想法如何实现?
我不想使用django-bootstrap3等,我也查看了他们的源代码,无法看到他们设法实现这一点的任何地方。 p>
事实证明,这比它看起来更加内在,涉及提供一个表单域混合以及一个自定义小部件。
所有太多的工作让我维持。
然而!
django-angular
已经设法通过自己的 CheckboxInput
和相关联的 BooleanFieldMixin
,因为这是我正在计划使用的,它已经解决了我问题。
I'm in the process of trying to minimize the amount of code I need to use to render a form with bootstrap styling with the hope of rendering with just {{ form }}
but I haven't yet managed to find a way to render a BooleanField
with the checkbox before the text.
from django.forms import Form, BooleanField
class MyForm(Form):
field = BooleanField(label='Test Label')
MyForm().as_table()
The above test code will output
<tr><th><label for="id_field">Test Label:</label></th><td><input class="" id="id_field" name="field" type="checkbox" /></td></tr>
But what I'm hoping to achieve is the same look and feel as shown in the bootstrap docs.
<label for="id_field"><input class="" id="id_field" name="field" type="checkbox" />Test Label:</label>
The problem in doing this is that the rendering is handled via the form, where the label and the field are positioned/rendered separately, and I have yet to find a place to override that will allow me to render the widget inside of the label...
Any ideas how I can achieve this?
I don't want to use django-bootstrap3 etc, and I've looked through the source code for them too and cant see anywhere where they've managed to achieve this either.
It turns out this is much more intrinsic to do than it would appear to be and involves providing a form field mixin as well as a custom widget.
All too much work for me to maintain.
However!
django-angular
has managed to achieve this with their own CheckboxInput
and associated BooleanFieldMixin
, since this is something I am planning on using, it has resolved my issue.
这篇关于在标签之前显示BooleanField复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!