django – ‘CheckoutView’对象没有属性’object’

django – ‘CheckoutView’对象没有属性’object’,第1张

概述我没有得到属性对象’错误’ 这是views.py class CheckoutView(FormMixin , DetailView): model = Cart template_name = "carts/checkout_view.html" form_class = GuestCheckoutForm def get_object(self , *ar 我没有得到属性’对象’错误’

这是vIEws.py

class CheckoutVIEw(FormMixin,DetailVIEw):    model = Cart    template_name = "carts/checkout_vIEw.HTML"    form_class = GuestCheckoutForm    def get_object(self,*args,**kwargs):        if self.request.user.is_authenticated():            try:                cart = Cart.objects.get(user__username=self.request.user)            except:                cart = None            if cart == None:                httpResponseRedirect(reverse("cart"))        else:            cart_ID = self.request.session.get("cart_ID")            if cart_ID == None:                httpResponseRedirect(reverse("cart"))            cart = Cart.objects.get(ID=cart_ID)        return cart    def get_context_data(self,**kwargs):        context = super(CheckoutVIEw,self).get_context_data(*args,**kwargs)        user_can_continue = False        if not self.request.user.is_authenticated():            context["login_form"] = AuthenticationForm()            context["next_url"] =  self.request.build_absolute_uri()        if self.request.user.is_authenticated():            user_can_continue = True        context["user_can_continue"] = user_can_continue        context["form"] = self.get_form()        return context    def post(self,request,**kwargs):        form = self.get_form()        if form.is_valID():            email = form.cleaned_data.get("email")            return self.form_valID(form)        else:            return self.form_invalID(form)    def get_success_url(self):        return reverse('checkout')

这是模板

<form method="POST" action="">{% csrf_token %}{{form | crispy }}<input type="submit"  value="continue"></form></div>

这是追溯

file "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response  132.                     response = wrapped_callback(request,*callback_args,**callback_kwargs)file "C:\Python27\lib\site-packages\django\vIEws\generic\base.py" in vIEw  71.             return self.dispatch(request,**kwargs)file "C:\Python27\lib\site-packages\django\vIEws\generic\base.py" in dispatch  89.         return handler(request,**kwargs)file "C:\Users\lenovo\Desktop\plump\Plumpin\src\carts\vIEws.py" in post  151.          return self.form_invalID(form)file "C:\Python27\lib\site-packages\django\vIEws\generic\edit.py" in form_invalID  115.         return self.render_to_response(self.get_context_data(form=form))file "C:\Users\lenovo\Desktop\plump\Plumpin\src\carts\vIEws.py" in get_context_data  133.      context = super(CheckoutVIEw,**kwargs)file "C:\Python27\lib\site-packages\django\vIEws\generic\detail.py" in get_context_data  101.         if self.object:Exception Type: AttributeError at /checkout/Exception Value: 'CheckoutVIEw' object has no attribute 'object'

我怎样才能解决上述问题?
在这里我尝试使用post方法提交表单,但我收到上述错误

谢谢

解决方法 您需要在视图的post方法中使用.get_object()将对象分配给视图.

这是因为Django的get_context_data()函数使用该对象将其传递到上下文中.如果POST请求出错,将调用此函数,它将查找您未分配的self.object,从而导致错误.

class CheckoutVIEw(FormMixin,DetailVIEw):    model = Cart    template_name = "carts/checkout_vIEw.HTML"    form_class = GuestCheckoutForm    ...    def post(self,**kwargs):        self.object = self.get_object() # assign the object to the vIEw        form = self.get_form()        if form.is_valID():            email = form.cleaned_data.get("email")            return self.form_valID(form)        else:            return self.form_invalID(form)

此外,最好在这里使用UpdateVIEw而不是DetailVIEw.

总结

以上是内存溢出为你收集整理的django – ‘CheckoutView’对象没有属性’object’全部内容,希望文章能够帮你解决django – ‘CheckoutView’对象没有属性’object’所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1195142.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存