【问题标题】:How to add placeholder UserCreationForm django?如何添加占位符 UserCreationForm django?
【发布时间】:2019-04-14 02:21:01
【问题描述】:

我正在尝试将占位符添加到 UserCreationForm 中的用户名字段 但我不明白该怎么做。 我已经以这种方式更改了forms.py文件/lib/site-packages/django/contrib/auth/forms.py。

我在 password1 和 password2 字段中添加了一个占位符,并像这样工作:

class UserCreationForm(forms.ModelForm):
"""
A form that creates a user, with no privileges, from the given username and
password.
"""
error_messages = {
    'password_mismatch': _("The two password fields didn't match."),
}
password1 = forms.CharField(
    label=_("Password"),
    strip=False,
    widget=forms.PasswordInput(attrs={'placeholder':'Password'}),
    help_text=password_validation.password_validators_help_text_html(),
)
password2 = forms.CharField(
    label=_("Password confirmation"),
    widget=forms.PasswordInput(attrs={'placeholder':'Confirmar Password'}),
    strip=False,
    help_text=_("Enter the same password as before, for verification."),
)

我可以看到用户名字段可能来自类元:

class Meta:
    model = User
    fields = ("username",)
    field_classes = {'username': UsernameField}

来自这门课,但我不确定

class UsernameField(forms.CharField):
def to_python(self, value):
    return unicodedata.normalize('NFKC', super().to_python(value))

我不明白如何在用户名字段中添加占位符

这是我的html

<form method="post" action=".">
                {% csrf_token %}
                    {% for field in form %}
                        {{ field }}<br />
                        {% for error in field.errors %}
                        <p style="color: red">{{ error }}</p>
                        {% endfor %} 
                    {% endfor %}
                <input id="submit-signup-btn" type="submit" value="Iniciar"/>    
            </form>

【问题讨论】:

    标签: django


    【解决方案1】:

    看了一会儿,我找到了一种方法,我就是这样做的:

    在 /lib/site-packages/django/contrib/auth/forms.py 文件中,在 UserCreationForm 类中,在 Meta 类下有一个 init 函数,您可以在其中看到这一行

    self.fields[self._meta.model.USERNAME_FIELD].widget.attrs.update({'autofocus': 
    True})
    

    我在更新方法中将占位符添加到行尾,如下所示:

    self.fields[self._meta.model.USERNAME_FIELD].widget.attrs.update({'autofocus': True,
                                                                              'placeholder':'Nome Perfil'})
    

    【讨论】:

      【解决方案2】:

      根据@Gabriel costa 的回答,我想出了这个解决方案:

      class AccountCreationForm(UserCreationForm):
      
      def __init__(self, *args, **kwargs):
          super().__init__(*args, **kwargs)
          self.fields['username'].widget.attrs.update({'placeholder':_('Username')})
          self.fields['email'].widget.attrs.update({'placeholder':_('Email')})
          self.fields['password1'].widget.attrs.update({'placeholder':_('Password')})        
          self.fields['password2'].widget.attrs.update({'placeholder':_('Repeat password')})
      

      其他解决方案在这种情况下不起作用。

      【讨论】:

      【解决方案3】:

      您还可以在表单的 Meta 类中定义小部件。

      class Meta:
          model = User
          fields = ("username",)
          field_classes = {'username': UsernameField}
          widgets = {
              'username': form.fields.TextInput(attrs={'placeholder': 'Your text for placeholder'})
          }
      

      【讨论】:

        【解决方案4】:

        我之前遇到过类似的问题,我就是这样解决的:

        class Meta:
           ......
           widgets = {
            'username': forms.fields.TextInput(attrs={'placeholder': 'random stuff'})
        }
        

        或内部,例如

        city = forms.CharField(widget=forms.TextInput(attrs={'placeholder':'text'}))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-12-23
          • 2012-11-11
          • 1970-01-01
          • 2016-10-04
          • 1970-01-01
          • 2014-03-13
          • 1970-01-01
          • 2020-11-26
          相关资源
          最近更新 更多