繁体   English   中英

Django脆皮表格:在复选框旁边添加文本?

[英]Django crispy forms: add text next to a checkbox?

这似乎很简单,但我不知道如何在复选框旁边添加一些文本。 我不想覆盖模板(我想我不需要)。 目前,我有一个可以使用的复选框,但是如何在该复选框的右侧放置一些文本,例如“我同意TOS”?

这是我所拥有的: https : //www.dropbox.com/s/i3n5rivhzfvo2ms/Screenshot%202015-06-30%2012.10.46.png?dl=0

class TenantSignupForm(forms.Form):
    email = forms.EmailField()
    password = forms.CharField(
        widget=forms.PasswordInput(),
        validators=[RegexValidator(regex=r'[A-Za-z0-9@#$%^&+=]{8,}', code=None),
                    MaxLengthValidator(32)])
    password_confirmation = forms.CharField(
        widget=forms.PasswordInput())
    agree_tos = forms.BooleanField()

    def __init__(self, *args, **kwargs):
        super(TenantSignupForm, self).__init__(*args, **kwargs)
        self.helper = unvariable_helper(self)
        self.helper.form_class = 'm-t'
        self.helper.form_action = 'signup'
        self.helper.layout = Layout(
            Field('email', placeholder="Email"),
            Field('password', placeholder="Password"),
            Field('password_confirmation', placeholder="Confirm your password"),
            Field('agree_tos', wrapper_class='i-checks'),
            FormActions(
                Submit('signup',
                       'Register',
                       css_class='btn btn-primary block full-width m-b'
                      )
                )
        )

我不熟悉help_text crispy-forms ,但可能添加了help_text可能会有所帮助

agree_tos = forms.BooleanField(help_text="I Agree TOS")

更新:另一个猜测

agree_tos = forms.BooleanField(label="I Agree TOS")

如果要在模板中分别显示每个“ 表单”窗口小部件 ,则可以在以下窗口小部件旁边添加所需的文本。

{{ form.agree_tos }} 

对于我来说,最好的方法是为此字段使用特定的模板:

Field('agree_tos', template="{0}/tos.html".format(TEMPLATE_PACK))

根据所使用的Template_pack,您必须查看相应目录(例如uni_form或bootstrap)中的默认模板(field.html),然后快速轻松地制作自己的模板。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM