【问题标题】:How i can remove help_text from a user creation form?如何从用户创建表单中删除 help_text?
【发布时间】:2015-02-09 17:11:55
【问题描述】:

当我使用 UserCreationForm 创建新用户时,我需要删除模板上显示的帮助文本。

我的意思是帮助文本:

对于用户名字段:Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.

和密码确认:Enter the same password as above, for verification。

来自help_text字段参数

【问题讨论】:

标签: django


【解决方案1】:

做了一个回答,其实你可以这样做

class UserCreateForm(UserCreationForm):
    email = forms.EmailField(required=True)
    email.help_text = ''

【讨论】:

    【解决方案2】:

    使用 init 你可以设置help_text None

    class UserCreateForm(UserCreationForm):
        email = forms.EmailField(required=True)
    
        def __init__(self, *args, **kwargs):
            super(UserCreateForm, self).__init__(*args, **kwargs)
                self.fields['email'].help_text = ''
    

    【讨论】:

    • 不是一个好方法。我们不想覆盖它,而只是隐藏它
    • @MihaiZamfir ...有什么区别?它只是在表单本身中被覆盖,而不是在模型中。
    • 其实你可以这样class UserCreateForm(UserCreationForm): email = forms.EmailField(required=True) email.help_text = ''
    • @batiskaf 这也是不错的方式..!你为什么不回答呢?我来自漫长的道路.. :)
    【解决方案3】:

    您可能正在使用 {{ form.as_p }} 之类的东西渲染您的模板,它会渲染所有内容、标签、help_texts、错误和字段本身。

    您可以这样做:

    {{ form.username.label }}
    {{ form.username }}
    ....
    

    这里有更多关于手动渲染表单的细节:https://docs.djangoproject.com/en/dev/topics/forms/#rendering-fields-manually

    【讨论】:

      猜你喜欢
      • 2012-10-23
      • 1970-01-01
      • 1970-01-01
      • 2019-12-10
      • 2019-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-09
      相关资源
      最近更新 更多