【问题标题】:Django crispy forms: add text next to a checkbox?Django 脆皮表单:在复选框旁边添加文本?
【发布时间】:2015-06-30 20:15:42
【问题描述】:

这似乎很简单,但我不知道如何在我的复选框旁边添加一些文本。我不想覆盖模板(我想我不需要)。 现在,我有一个可以使用的复选框,但是如何在复选框右侧放置一些文本,例如“我同意 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'
                      )
                )
        )

【问题讨论】:

标签: python django checkbox django-crispy-forms


【解决方案1】:

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

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

更新:另一个猜测

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

【讨论】:

  • 这是一个很好的尝试,但不幸的是,这会将文本添加到 div 之外,如您所见 there
  • 阿夫。如果我设置form.helper.form_show_labels = True,它会起作用,但我想避免为文本输入字段添加标签。 Take a look here please
  • 感谢您的想法,我找到了一种方法。如果我为除agree_tos以外的所有字段设置label="",它将按预期工作。但是,对于大多数字段重复 label="" 并不是很干燥。我想以另一种方式做到这一点:设置form.helper.form_show_labels = False 然后仅为我的复选框覆盖它。任何想法?谢谢!
【解决方案2】:

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

{{ form.agree_tos }} 

【讨论】:

  • 不,我使用{% crispy form %} 来显示整个表单(我想继续这样做)。
【解决方案3】:

就我而言,最好的方法是为此字段使用特定模板:

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

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

【讨论】:

    猜你喜欢
    • 2014-12-17
    • 1970-01-01
    • 2020-08-21
    • 1970-01-01
    • 2014-10-27
    • 2014-02-28
    • 1970-01-01
    • 2019-07-27
    • 2013-10-29
    相关资源
    最近更新 更多