'CheckoutView' object has no attribute 'object'

给你一囗甜甜゛ 提交于 2019-12-04 22:08:07

问题


I am getting no attribute 'object' error'

here is 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 ,*args , **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 , *args , **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')

here is the template

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

here is the traceback

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, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\views\generic\base.py" in dispatch
  89.         return handler(request, *args, **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 , self).get_context_data(*args , **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'

how can i solve the above problem? here i am trying submit the form using post method but i am getting the above error

Thank You


回答1:


You need to assign object to your view using .get_object() in the post method of your view.

This is because Django's get_context_data() function uses the object to pass it into the context. In case of errors in POST request, this function will be called and it will look for self.object which you did not assign, thereby leading to the error.

class CheckoutView(FormMixin , DetailView):
    model = Cart
    template_name = "carts/checkout_view.html"
    form_class = GuestCheckoutForm

    ...

    def post(self , request , *args , **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)

Also, it would be better to use UpdateView here instead of DetailView.



来源:https://stackoverflow.com/questions/34460708/checkoutview-object-has-no-attribute-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!