gpt4 book ai didi

python - Django - 表单错误显示不正确

转载 作者:太空宇宙 更新时间:2023-11-03 17:18:42 24 4
gpt4 key购买 nike

当我尝试显示表单错误字段时,我得到了非常奇怪的行为: enter image description here

我几乎尝试了代码的所有组合,但无法理解我做错了什么。如何在表单上显示错误?

我有这样的设置:

base.html:

<head>
...
</head>
<body>
<header>
{% if user.is_anonymous %}
<div class="welcome">
<span>Welcome!</span>
</div>
{% else %}
<div class="wrapper">
<a href="/">Home</a>
<a href="/users/">Public Profiles</a>
<a href="/users/{{ username }}">My Profile</a>
<a href="/ribbits">Public Ribbits</a>

<form action="/logout">
<input type="submit" id="btnLogOut" value="Log Out">
</form>
</div>
{% endif %}
</header>
<div id="content">
<div class="wrapper">
{% block flash %}
{% if auth_form.non_field_errors or user_form.non_field_errors or ribbit_form.errors %}
<div class="flash error">
{{ auth_form.non_field_errors }}
{{ user_form.non_field_errors }}
{{ ribbit_form.content.errors }}
</div>
{% endif %}
{% if notice %}
<div class="flash notice">
{{ notice }}
</div>
{% endif %}
{% endblock %}
{% block content %}
{% endblock %}
</div>
</div>
</body>
</html>

注册.html: {% 扩展“base.html”%}

{% block content %}
<div class="panel right">
<h1>New to Ribbit?</h1>
<p>
<form action="/signup" method="post">{% csrf_token %}
{% for field in user_form %}
{{ field }}
{% endfor %}
<input type="submit" value="Create Account">
</form>
</p>
</div>
{% endblock %}

表格:

class UserCreateForm(UserCreationForm):
email = forms.EmailField(required=True, widget=forms.widgets.TextInput(attrs={'placeholder': 'Email'}))
first_name = forms.CharField(required=True, widget=forms.widgets.TextInput(attrs={'placeholder': 'First Name'}))
last_name = forms.CharField(required=True, widget=forms.widgets.TextInput(attrs={'placeholder': 'Last Name'}))
username = forms.CharField(widget=forms.widgets.TextInput(attrs={'placeholder': 'Username'}))
password1 = forms.CharField(widget=forms.widgets.PasswordInput(attrs={'placeholder': 'Password'}))
password2 = forms.CharField(widget=forms.widgets.PasswordInput(attrs={'placeholder': 'Password Confirmation'}))

def is_valid(self):
form = super(UserCreateForm, self).is_valid()
for f, error in self.errors.iteritems():
if f != '__all_':
self.fields[f].widget.attrs.update({'class': 'error', 'value': strip_tags(error)})
return form

class Meta:
fields = ['email', 'username', 'first_name', 'last_name', 'password1',
'password2']
model = User

查看:

def signup(request):
user_form = UserCreateForm(data=request.POST)
if request.method == 'POST':
if user_form.is_valid():
username = user_form.cleaned_data['username']
password = user_form.cleaned_data['password2']
user_form.save()
user = authenticate(username=username, password=password)
login(request, user)
return redirect('/')
return register(request, user_form=user_form)


def register(request, user_form=None):
form = user_form or UserCreateForm()
return render(request, 'register.html', {'user_form': form})

最佳答案

rendering forms manually 的 Django 文档涵盖如何显示错误。

您的行为很奇怪,因为您通过覆盖 is_valid() 方法做了非常奇怪的事情。

它不起作用,因为 error 是一个 ErrorList 实例,而不是字符串。当您执行 strip_tags(error) 时,它不会按照您的预期剥离标签。

实际上,您正在替换

<ul class="errorlist"><li>This field is required.</li></ul>

进入

<input value="<value goes here>" />

结果是

<input value="<ul class=" errorlist">
<li>This field is required.</li></ul>
" />

当你的浏览器显示时,这看起来很奇怪。

如果您确实希望将错误显示为表单值,则需要将 ErrorList 转换为字符串。您可以使用

获取错误字符串列表,而不是尝试 strip_tags(errors) (注意 error s 因为可能有多个)
[error for error in errors]

关于python - Django - 表单错误显示不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33402093/

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