'CheckoutView' object has no attribute 'object'

若如初见. 提交于 2019-12-03 15:24:42

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.

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