Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'CheckoutView' object has no attribute 'object'

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

like image 796
asing177 Avatar asked Dec 25 '15 06:12

asing177


2 Answers

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.

like image 150
Rahul Gupta Avatar answered Oct 16 '22 22:10

Rahul Gupta


For me the solution was to initialize self.object to [] (empty list) in get_context_data.

like image 1
Emmanuel Chamilakis Avatar answered Oct 16 '22 22:10

Emmanuel Chamilakis