2
我越來越沒有屬性 '對象' 錯誤」'CheckoutView' 對象有沒有屬性 '對象'
這裏是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'
ho我能解決上述問題嗎? 在這裏,我嘗試使用POST方法提交表單,但我得到上述錯誤
謝謝
謝謝,我錯過了一個 –
您可以嘗試'UpdateView',因爲它會更新一個對象,並重新顯示錯誤的頁面,如果有的話。 –
感謝rahul gupta –