表单错误未显示在UpdateView中

表单错误未显示在UpdateView中

本文介绍了表单错误未显示在UpdateView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UpdateView,它将显示一个表单来创建用户配置文件或更新用户配置文件。效果很好,但是由于某种原因,我无法显示表单错误。基于我在模型表单中输入的 ValidationErrors ,应该存在表单错误。我怀疑我的views.py是导致表单未显示错误的原因。

I have an UpdateView that will display a form to either create the user profile or update the user profile. It works well, however, for some reason I can't get it to show the form errors. There should be form errors based on the ValidationErrors I have put in the model form. I suspect my views.py to be the cause of the form not displaying the errors.

这是我的观点:

class ProfileSettings(UpdateView):
    model = Profile
    template_name = 'profile/settings.html'
    form_class = ProfileForm
    success_url = reverse_lazy('profile:settings')

    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST, request.FILES)

        if form.is_valid():
            bio = form.cleaned_data['bio']
            gender = form.cleaned_data['gender']
            avatar = form.cleaned_data['avatar']


            Profile.objects.update_or_create(user=self.request.user, defaults={'avatar':avatar, 'bio':bio, 'gender':gender})

        return HttpResponseRedirect(self.success_url)

模板,我正在显示以下错误: {{form.avatar.errors}} 其中,如果图像太小会显示错误,但不是。

In the template, I am displaying the errors like so: {{ form.avatar.errors }} where avatar should display an error if the image is too small, but it doesn't.

推荐答案

在这里,您没有返回表单模板错误。我认为您可以这样处理(覆盖 form_valid 而不是 post 方法):

Here, you are not returning the form errors to the template. I think you can approach like this(overriding form_valid instead of post method):

class ProfileSettings(UpdateView):
    model = Profile
    template_name = 'profile/settings.html'
    form_class = ProfileForm
    success_url = reverse_lazy('profile:settings')

   def form_valid(self, form):
      bio = form.cleaned_data['bio']
      gender = form.cleaned_data['gender']
      avatar = form.cleaned_data['avatar']
      Profile.objects.update_or_create(user=self.request.user, defaults={'avatar':avatar, 'bio':bio, 'gender':gender})
      return HttpResponseRedirect(self.success_url)

顺便说一句,此代码也存在一些缺陷。我们此处未使用模型表单的保存。最好是可以使用它。 form_valid 最初是这样做的。

BTW, there are some flaws with this code as well. We are not using model form's save here. It would be best if we could use that. form_valid originally does that.

另外,我想最好使用可以在创建用户时创建用户个人资料。这是有关如何操作的。这样,您甚至不需要覆盖 form_valid

Also on another note, I think its best to use signals to create the user profile whenever the user is created. Here is a medium post on how to do that. In that way, you don't even need to override form_valid.

使用FormView预先填充数据:

Using FormView to pre-populate data:

class ProfileSettings(FormView):
    model = Profile
    template_name = 'profile/settings.html'
    form_class = ProfileForm
    success_url = reverse_lazy('profile:settings')

   def get_form_kwargs(self):
        kwargs = super(ProfileSettings, self).get_form_kwargs()
        kwargs['instance'] = self.request.user.profile  # Profile object
        return kwargs

这篇关于表单错误未显示在UpdateView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 20:24