[英]Removing help_text from Django UserCreateForm
可能是一個糟糕的問題,但我正在使用 Django 的 UserCreationForm(稍作修改以包含電子郵件),並且我想刪除 Django 自動顯示在 HTML 頁面上的 help_text。
在我的 HTML 頁面的注冊部分,它有用戶名、電子郵件、密碼 1 和密碼 2 字段。 但是在用戶名下面是“必需。30 個字符或更少。字母、數字和@...”。 在密碼確認(密碼 2)下,它顯示“輸入與上述相同的密碼進行驗證”。
我如何刪除這些?
#models.py
class UserCreateForm(UserCreationForm):
email = forms.EmailField(required=True)
def save(self, commit=True):
user = super(UserCreateForm, self).save(commit=False)
user.email = self.cleaned_data['email']
if commit:
user.save()
return user
class Meta:
model = User
fields = ("username", "email", "password1", "password2")
exclude = ('username.help_text')
#views.py
def index(request):
r = Movie.objects.all().order_by('-pub_date')
form = UserCreateForm()
return render_to_response('qanda/index.html', {'latest_movie_list': r, 'form':form}, context_instance = RequestContext(request))
#index.html
<form action = "/home/register/" method = "post" id = "register">{% csrf_token %}
<h6> Create an account </h6>
{{ form.as_p }}
<input type = "submit" value = "Create!">
<input type = "hidden" name = "next" value = "{{ next|escape }}" />
</form>
您可以在__init__
help_text
字段的help_text
設置為 None
from django.contrib.auth.forms import UserCreationForm
from django import forms
class UserCreateForm(UserCreationForm):
email = forms.EmailField(required=True)
def __init__(self, *args, **kwargs):
super(UserCreateForm, self).__init__(*args, **kwargs)
for fieldname in ['username', 'password1', 'password2']:
self.fields[fieldname].help_text = None
print UserCreateForm()
輸出:
<tr><th><label for="id_username">Username:</label></th><td><input id="id_username" type="text" name="username" maxlength="30" /></td></tr>
<tr><th><label for="id_password1">Password:</label></th><td><input type="password" name="password1" id="id_password1" /></td></tr>
<tr><th><label for="id_password2">Password confirmation:</label></th><td><input type="password" name="password2" id="id_password2" /></td></tr>
<tr><th><label for="id_email">Email:</label></th><td><input type="text" name="email" id="id_email" /></td></tr>
如果您進行了太多更改,在這種情況下,最好僅覆蓋字段,例如
class UserCreateForm(UserCreationForm):
password2 = forms.CharField(label=_("Whatever"), widget=MyPasswordInput
但在您的情況下,我的解決方案將非常有效。
另一個更簡潔的選擇是在 Meta 類中使用 help_texts 字典。 例子:
class UserCreateForm(UserCreationForm):
...
class Meta:
model = User
fields = ("username", "email", "password1", "password2")
help_texts = {
'username': None,
'email': None,
}
更多信息在這里: https : //docs.djangoproject.com/en/1.11/topics/forms/modelforms/#overriding-the-default-fields
適用於用戶名和電子郵件,但不適用於 password2。 不知道為什么。
您可以像這樣將 css 類添加到 registration_form.html 文件。
<style> .helptext{ visibility: hidden; } </style>
簡單的 CSS 解決方案。
<style>
#hint_id_username, #hint_id_password1 {
display: none;
}
</style>
當表單呈現檢查頁面源代碼時,您將看到每個幫助文本的 id。 例如每個表單字段的hint_id_username
。 使用上面的 CSS 隱藏文本。
或者只是遍歷表單字段並省略輸出“field.help_text”
{% for field in form %}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.label_tag }} {{ field }}
<!--
{% if field.help_text %}
<p class="help">{{ field.help_text|safe }}</p>
{% endif %}
-->
</div>
{% endfor %}
Django 文檔: https : //docs.djangoproject.com/en/3.0/topics/forms/#looping-over-the-form-s-fields
我有一個類似的問題。 根據其中一條評論,這是閱讀文檔后的解決方案。
class UserCreateForm(UserCreationForm):
password1 = forms.CharField(label='Enter password',
widget=forms.PasswordInput)
password2 = forms.CharField(label='Confirm password',
widget=forms.PasswordInput)
class Meta:
model=User
fields=("username","email","first_name",
"last_name","password1","password2")
help_texts = {
"username":None,
}
基本上我們要做的是通過為我們的新類表單重新創建密碼字段來覆蓋自動設置。
只需轉到 UserCreationForm 並進行所需的更改。
非常簡單地按住鍵盤上的控制按鈕並單擊 UserCreationForm,您將獲得 UserCreationForm 根據您的需要進行所需的更改並保存它。 正如我在下面的示例中為我所做的那樣,我評論了幫助內容。
error_messages = {
'password_mismatch': _("The two password fields didn't match."),
}
password1 = forms.CharField(
label=_("Password"),
strip=False,
widget=forms.PasswordInput,
# help_text=password_validation.password_validators_help_text_html(),
)
password2 = forms.CharField(
label=_("Password confirmation"),
widget=forms.PasswordInput,
strip=False,
help_text=_("Enter the same password as before, for verification."),
)
對於那些想要更改密碼 1 和 2 的默認文本而不必重新創建默認模型的人,可以像我一樣嘗試這樣做。 只需在您的類元下添加此 init 函數即可。
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['password1'].help_text='Your text'
self.fields['password2'].help_text='Your text'
只需覆蓋該字段的 help_text 屬性。
例如
username = forms.CharField(help_text=None)
您不需要更改任何其他參數。 它會正常工作。
添加此 CSS 以刪除幫助文本。
<style>
.helptext {
visibility: hidden;
}
body > main > form > ul > li{
display: none;
}
</style>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.