我創建了一個訂單頁面,客戶可以查看他在過去完成的所有訂單,但由於我希望客戶登錄並查看訂單,所以我創建了一個loginrequiredmixin當我嘗試以用戶身份登錄時在mixin中出現錯誤
mixins.py
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from carts.models import Cart
from .models import Order
class LoginRequiredMixin(object):
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
return super(LoginRequiredMixin, self).dispatch(request,*args, **kwargs)
class CartOrderMixin(object):
def get_order(self, *args, **kwargs):
cart = self.get_cart()
if cart is None:
return None
new_order_id = self.request.session.get("order_id")
if new_order_id is None:
new_order = Order.objects.create(cart=cart)
self.request.session["order_id"] = new_order.id
else:
new_order = Order.objects.get(id=new_order_id)
return new_order
def get_cart(self, *args, **kwargs):
cart_id = self.request.session.get("cart_id")
if cart_id == None:
return None
cart = Cart.objects.get(id=cart_id)
if cart.items.count() <= 0:
return None
return cart
views.py
from django.shortcuts import render
from django.views.generic.edit import CreateView, FormView
from django.views.generic.list import ListView
# Create your views here.
from .mixins import CartOrderMixin, LoginRequiredMixin
from .models import UserCheckout, Order
class OrderList(LoginRequiredMixin, ListView):
queryset = Order.objects.all()
def get_queryset(self):
user_check_id = self.request.user.id
user_checkout = UserCheckout.objects.get(id=user_check_id)
return super(OrderList, self).get_queryset().filter(user=user_checkout)
models.py
from decimal import Decimal
from django.conf import settings
from django.db import models
from django.db.models.signals import pre_save
# Create your models here.
from carts.models import Cart
class UserCheckout(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, null=True, blank=True) #not required
email = models.EmailField(unique=True) #--> required
def __unicode__(self): #def __str__(self):
return self.email
ORDER_STATUS_CHOICES = (
('created', 'Created'),
('completed', 'Completed')
)
class Order(models.Model):
status = models.CharField(max_length=120, choices=ORDER_STATUS_CHOICES, default ='created')
cart = models.ForeignKey(Cart)
user = models.ForeignKey(UserCheckout, null=True)
#shipping_address = models.ForeignKey
#final_total_price = models.DecimalField(default=)
order_total = models.DecimalField(max_digits=50, decimal_places=2,)
#orderid
def __unicode__(self):
return str(self.cart.id)
def mark_completed(self):
self.status = "completed"
self.save()
def order_pre_save(sender, instance, *args, **kwargs):
cart_total = instance.cart.total
order_total = Decimal(cart_total)
instance.order_total = order_total
pre_save.connect(order_pre_save, sender=Order)
錯誤
DoesNotExist at /orders/
UserCheckout matching query does not exist.
Request Method: GET
Request URL: http://127.0.0.1:8000/orders/
Django Version: 1.8.4
Exception Type: DoesNotExist
Exception Value:
UserCheckout matching query does not exist.
Exception Location: /Users/apulgupta/Desktop/ecommerce-2/Try-Django-1.8/lib/python2.7/site-packages/django/db/models/query.py in get, line 334
Python Executable: /Users/apulgupta/Desktop/ecommerce-2/Try-Django-1.8/bin/python
Python Version: 2.7.10
Python Path:
['/Users/apulgupta/Documents/ecommerce-2-before-guest-save/Try-Django-1.8/src',
'/Users/apulgupta/Desktop/ecommerce-2/Try-Django-1.8/lib/python27.zip',
'/Users/apulgupta/Desktop/ecommerce-2/Try-Django-1.8/lib/python2.7',
'/Users/apulgupta/Desktop/ecommerce-2/Try-Django-1.8/lib/python2.7/plat-darwin',
'/Users/apulgupta/Desktop/ecommerce-2/Try-Django-1.8/lib/python2.7/plat-mac',
'/Users/apulgupta/Desktop/ecommerce-2/Try-Django-1.8/lib/python2.7/plat-mac/lib-scriptpackages',
'/Users/apulgupta/Desktop/ecommerce-2/Try-Django-1.8/lib/python2.7/lib-tk',
'/Users/apulgupta/Desktop/ecommerce-2/Try-Django-1.8/lib/python2.7/lib-old',
'/Users/apulgupta/Desktop/ecommerce-2/Try-Django-1.8/lib/python2.7/lib-dynload',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/Users/apulgupta/Desktop/ecommerce-2/Try-Django-1.8/lib/python2.7/site-packages']
Server time: Wed, 15 Mar 2017 17:42:22 +0000
只要我得到的登錄頁面,我嘗試登錄我得到上述錯誤
你可以用'UserCheckout.objects.get_or_create '如果你想讓用戶總是有一個關聯的'UserCheckout'對象 –
我嘗試了上面的解決方案,但它也拋出了404頁面 –
還做了一個get_or_create請求我得到一個類型錯誤,指出int()參數必須是一個字符串或數字,而不是'UserCheckout' –