' CheckoutView'对象没有属性' object'

时间:2015-12-25 06:54:01

标签: django django-forms django-views

我没有任何属性' object'错误'

这里是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')

这是模板

<form method="POST" action="">
{% csrf_token %}
{{form | crispy }}
<input type="submit" class="btn btn-success" 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, *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'

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

谢谢

3 个答案:

答案 0 :(得分:19)

您需要在视图的object方法中使用.get_object() 为您的视图指定post

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

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)

此外,使用UpdateView此处代替DetailView 会更好。

答案 1 :(得分:1)

对我来说,解决方案是将self.object初始化为get_context_data中的[](空列表)。

答案 2 :(得分:0)

记住这些是类,而 self 就像类的灵魂。

def get(self, request, id):
        user_g = **self**.get_object(id)