Python Django updateview表单验证错误未显示在模板中
我在views.py中有一个更新视图Python Django updateview表单验证错误未显示在模板中,python,django,django-forms,django-views,Python,Django,Django Forms,Django Views,我在views.py中有一个更新视图 class UserProfileUpdateView(LoginRequiredMixin, UpdateView): model = UserProfile template_name = 'my-account/my_profile_update.html' form_class = UserProfileUpdateForm def get_context_data(self, **kwargs):
class UserProfileUpdateView(LoginRequiredMixin, UpdateView):
model = UserProfile
template_name = 'my-account/my_profile_update.html'
form_class = UserProfileUpdateForm
def get_context_data(self, **kwargs):
context = super(UserProfileUpdateView, self).get_context_data(**kwargs)
context['form'] = UserProfileUpdateForm(instance=UserProfile.objects.get(user=self.request.user))
return context
def get_object(self):
return get_object_or_404(UserProfile, user=self.request.user)
class UserProfileUpdateForm(forms.ModelForm):
username = forms.CharField(label='Username')
video = forms.URLField(required=False, label='Profile Video')
def clean_username(self):
username = self.cleaned_data['username']
if UserProfile.objects.filter(username=username).exists():
print "This print is working"
raise forms.ValidationError('Username already exists.')
return username
class Meta:
model = UserProfile
在表单中.py
class UserProfileUpdateView(LoginRequiredMixin, UpdateView):
model = UserProfile
template_name = 'my-account/my_profile_update.html'
form_class = UserProfileUpdateForm
def get_context_data(self, **kwargs):
context = super(UserProfileUpdateView, self).get_context_data(**kwargs)
context['form'] = UserProfileUpdateForm(instance=UserProfile.objects.get(user=self.request.user))
return context
def get_object(self):
return get_object_or_404(UserProfile, user=self.request.user)
class UserProfileUpdateForm(forms.ModelForm):
username = forms.CharField(label='Username')
video = forms.URLField(required=False, label='Profile Video')
def clean_username(self):
username = self.cleaned_data['username']
if UserProfile.objects.filter(username=username).exists():
print "This print is working"
raise forms.ValidationError('Username already exists.')
return username
class Meta:
model = UserProfile
但模板中的表单错误不显示
在模板home.html中
{{ form.username.errors }}
输入现有用户验证并引发错误,但不显示在form.username.errors中时。我尝试打印表单,但表单上未发现错误。这就是updateview的问题吗
提前感谢。更新视图已将表单包含在上下文中。但是,在您的
get\u context\u data
方法中,您将用
context['form'] = UserProfileUpdateForm(instance=UserProfile.objects.get(user=self.request.user))
此表单不绑定post数据,因此它永远不会有任何错误
你不需要包括这一行。您的
get\u对象
方法应该足以确保您的视图使用正确的用户。在您的情况下,UserProfileUpdateForm
已经绑定了UserProfile
,因此您不需要更改上下文
数据
然而,当我试图通过以下方法为表单提供一些初始值时,我面临着完全相同的问题。所以在获取上下文数据中,我有
context['form'] = self.form_class(instance=self.post, initial={"tags":",".join([tag.name for tag in self.post.tags.all()])})
这将使用与post相关联的标签列表(用逗号分隔)预填充form.tags
我在潜入UpdateView
之后设法解决了这个问题。在第81行,他们有
def form_invalid(self, form):
"""
If the form is invalid, re-render the context data with the
data-filled form and errors.
"""
return self.render_to_response(self.get_context_data(form=form))
如果表单无效且包含错误,它将使用绑定表单调用get\u context\u data
。我必须将此表单传递给模板,而不是在get\u context\u data
方法中指定的表单。
为了实现这一点,我们需要对get\u context\u data
进行一些更改
def get_context_data(self, **kwargs):
context = super(PostUpdate, self).get_context_data(**kwargs)
if 'form' in kwargs and kwargs['form'].errors:
return context
else:
context['form'] = self.form_class(instance=self.post, initial={"tags":",".join([tag.name for tag in self.post.tags.all()])})
return context
如果表单包含错误,它将直接将其传递给模板。否则,请使用我们提供的
我相信还有其他解决办法。如果你有,请把它寄出去。这将有助于其他人学习Django。我加入这一行是为了在表单中预先填充当前用户详细信息。这一行正是导致您出现问题的原因。表单应该已经使用该用户预先填充表单,因为您已经覆盖了get\u object
。谢谢,…如果执行post操作,是否有任何方法可以发送post表单?是的,理论上您可以检查方法类型并将表单绑定到post数据,但我不建议这样做。您不应该在get\u context\u data
方法中替换表单。