简体   繁体   English

Django 1.11-CreateView-无效的表单针对不存在的对象属性引发错误

[英]Django 1.11 - CreateView - invalid form raises error for nonexistent object attribute

Django 1.11, using generic class based views. Django 1.11,使用基于通用类的视图。

When submitting an invalid form then the call super(CreateSupport, self).get_context_data(**kwargs) raises 提交无效表单时,调用super(CreateSupport,self).get_context_data(** kwargs)引发

AttributeError: 'CreateSupport' object has no attribute 'object'. AttributeError:'CreateSupport'对象没有属性'object'。

Expecting to show validation errors on form instead. 期望在表单上显示验证错误。

View/Create when form is valid works alright. 表单有效时,可以查看/创建。

This is the class: 这是课程:

class CreateSupport(IsAdminMixin, CreateView):
model = Support
form_class = SupportForm
template_name = 'admin/support/form.html'
success_url = reverse_lazy('admin-supports')

def get_context_data(self, **kwargs):
    context = super(CreateSupport, self).get_context_data(**kwargs)
    if hasattr(self, 'object'):
        context['images_form'] = SupportForm.ProductImageFormSet(
            instance=self.object) if "validated_images_form" not in kwargs else kwargs["validated_images_form"]

    # filter the colour and size options for current vendor
    curr_vend = Vendor.objects.get(id=self.kwargs['vendorid'])
    context['colours'] = Colour.objects.filter(vendor=curr_vend)
    context['sizes'] = Size.objects.filter(vendor=curr_vend)
    return context

def form_valid(self, support_form):
    if support_form.is_valid():
        redirect = super(CreateSupport, self).form_valid(support_form)
    else:
        validated_forms_context = self.get_context_data(form=support_form)
        redirect = self.render_to_response(validated_forms_context)

    return redirect

def post(self, request, *args, **kwargs):
    support_form = SupportForm(data=request.POST)
    return self.form_valid(support_form)

I could not find any solution to this so far other than this old post using self.get_object() method that obviously would not work since the object has not yet been created. 除了使用self.get_object()方法的旧帖子外 ,到目前为止,我找不到任何解决方案,因为该对象尚未创建,因此显然无法使用。

Traceback: 追溯:

"C:\Program Files\JetBrains\PyCharm 2017.3.3\helpers\pydev\_pydevd_bundle\pydevd_exec2.py", line 3, in Exec
    exec(exp, global_vars, local_vars)
  File "<input>", line 1, in <module>
  File "C:\ProgramData\Anaconda2\envs\django3.4\lib\site-packages\django\views\generic\edit.py", line 94, in get_context_data
    return super(FormMixin, self).get_context_data(**kwargs)
  File "C:\ProgramData\Anaconda2\envs\django3.4\lib\site-packages\django\views\generic\detail.py", line 101, in get_context_data
    if self.object:
AttributeError: 'CreateSupport' object has no attribute 'object'

Following Will Keeling's comment about the default post() implementation, I reached out to BaseCreateView class. 在Will Keeling对默认post()实现发表评论之后,我接触了BaseCreateView类。

This is the default implementation of post(): 这是post()的默认实现:

def post(self, request, *args, **kwargs):
    self.object = None
    return super(BaseCreateView, self).post(request, *args, **kwargs)

The problem can thus be simply resolved by setting the self.object to None. 通过将self.object设置为None,可以简单地解决该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM