gpt4 book ai didi

python - 从 Django UserCreateForm 中删除 help_text

转载 作者:IT老高 更新时间:2023-10-28 20:50:33 27 4
gpt4 key购买 nike

可能是一个糟糕的问题,但我正在使用 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 设置为 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

但在你的情况下,我的解决方案会很好用。

关于python - 从 Django UserCreateForm 中删除 help_text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13202845/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com