[英]TypeError at /cart/ cannot unpack non-iterable Cart object
I am developing an eCommerce app in Django.我正在 Django 中开发电子商务应用程序。 I am still developing locally.
我还在本地开发。
As I try to access to http://127.0.0.1:8000/cart/
I get the error:当我尝试访问
http://127.0.0.1:8000/cart/
时,出现错误:
TypeError at /cart/
/cart/ 处的类型错误
cannot unpack non-iterable Cart object
无法解压不可迭代的购物车 object
in cart_home cart_obj, new_obj = Cart.objects.new_or_get(request) TypeError: cannot unpack non-iterable Cart object variable value request <WSGIRequest: GET '/cart/'>
and I can't understand what is wrong.我不明白出了什么问题。
Here is my models.py :这是我的models.py :
from django.db import models
from django.conf import settings
from products.models import Product
User = settings.AUTH_USER_MODEL
class CartManager(models.Manager):
def new_or_get(self, request):
cart_id = request.session.get("cart_id", None)
print(cart_id)
qs = self.get_queryset().filter(id=cart_id)
if qs.count() == 1 :
new_obj = False
print('Cart ID exists')
cart_obj = qs.first()
if request.user.is_authenticated and cart_obj.user is None:
cart_obj.user = request.user
cart_obj.save()
else:
print('Cart ID does not exist')
cart_obj = Cart.objects.new(user=request.user)
new_obj = True
request.session['cart_id'] = cart_obj.id
return cart_obj
def new(self, user=None):
user_obj = None
if user is not None:
if user.is_authenticated:
user_obj = user
return self.model.objects.create(user=user_obj)
class Cart(models.Model):
user = models.ForeignKey(User, null=True, blank=True, on_delete=models.CASCADE)
products = models.ManyToManyField(Product, blank=True)
total = models.DecimalField(default=0.00, max_digits=100, decimal_places=2)
timestamp = models.DateTimeField(auto_now=True)
updated = models.DateTimeField(auto_now_add=True)
objects = CartManager()
def __str__(self):
return str(self.id)
Here is my views.py :这是我的views.py :
from django.shortcuts import render
from .models import Cart
def cart_home(request):
cart_obj, new_obj = Cart.objects.new_or_get(request)
products = cart_obj.products.all()
total = 0
for x in products:
total += x.price
print("total")
print(total)
cart_obj.total = total
cart_obj.save()
return render(request, "carts/home.html", {})
Turns out that I forgot to add an argument ( new_obj
) in the return statement of models.py > CartManager > new_or_get
原来我忘了在
models.py > CartManager > new_or_get
的返回语句中添加一个参数( new_obj
)
so it returned just one argument, so that my command at views.py > cart_home
was trying to assing just one value ( cart_obj
) to two variables ( cart_obj, new_obj
).所以它只返回一个参数,因此我在
views.py > cart_home
处的命令试图将一个值( cart_obj
)分配给两个变量( cart_obj, new_obj
)。
So the correction on models.py > CartManager > new_or_get
is the following所以对
models.py > CartManager > new_or_get
的更正如下
def new_or_get(self, request):
cart_id = request.session.get("cart_id", None)
print(cart_id)
qs = self.get_queryset().filter(id=cart_id)
if qs.count() == 1 :
new_obj = False
print('Cart ID exists')
cart_obj = qs.first()
if request.user.is_authenticated and cart_obj.user is None:
cart_obj.user = request.user
cart_obj.save()
else:
print('Cart ID does not exist')
cart_obj = Cart.objects.new(user=request.user)
new_obj = True
request.session['cart_id'] = cart_obj.id
return cart_obj, new_obj
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.