在标签前显示 BooleanField 复选框 [英] Show BooleanField checkbox before label

查看:23
本文介绍了在标签前显示 BooleanField 复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试尽量减少使用引导程序样式呈现表单所需的代码量,希望仅使用 {{ form }} 进行呈现,但我还没有尚未设法找到一种方法来呈现 BooleanField 与文本前的复选框.

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

上面的测试代码会输出

<tr><th><label for="id_field">Test Label:</label></th><td><input class="" id="id_field" name="field" type="checkbox" /></td></tr>

但我希望实现的是与 bootstrap 文档中所示相同的外观和感觉.

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?

我不想使用 django-bootstrap3 等,而且我也查看了它们的源代码,但也看不到他们设法实现这一目标的任何地方.

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.

但是!

django-angular 已经管理用他们自己的CheckboxInput 和相关的 BooleanFieldMixin,因为这是我打算使用的东西,它已经解决了我的问题.

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屋!

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