【发布时间】: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