我想将产品添加到我的购物车并显示购物车详细信息,如购物车列表。但是代码没有在页面上显示详细信息?

I want to add products to my cart and display the cart details like cart list. but the code is not showing the details on the page?


浏览量写成

def cart_details(request, tot=0, count=0, cart_items=None):
try:ct = cartlist.objects.get(cart_id=c_id(request))
   ct_items = item.objects.filter(cart=ct, active=True)
   for i in ct_items:
       tot += (i.prodt.price * i.quan)count += i.quan
except ObjectDoesNotExist:
   pass
return render(request, 'cart.html', {'ci': cart_items, 't': tot, 'cn': count})

def c_id(request):
ct_id = request.session.session_key
if not ct_id:
   ct_id = request.session.create()
return ct_id

购物车>型号

class cartlist(models.Model):
cart_id = models.CharField(max_length=250, unique=True)
date_added = models.DateTimeField(auto_now_add=True)

class item(models.Model):
prodt = models.ForeignKey(product, on_delete=models.CASCADE)
cart = models.ForeignKey(cartlist, on_delete=models.CASCADE)
quan = models.IntegerField()
active = models.BooleanField(default=True)

购物车>网址

urlpatterns = ['
path('cartDetails', views.cart_details, name='cartDetails'),
path('add/<int:product_id>/', views.add_cart, name='addcart'),
]

cart.html

                  <tr>
                  {% for i in ci %}                                                                                                                     
                    <td><a href="#"><img src="{{i.prodt.img.url}}" alt="img"></a></td>
                    <td><a class="aa-cart-title" href="#">{{ i.prodt.name }}</a></td>
                    <td>${{ i.prodt.price }}</td>
                  {% endfor %}
                  <tr>

这是获取视图代码的购物车页面有一些错误,在添加产品时创建购物车 ID 然后添加产品但未显示在图表中 HTML 页面

除了默认值 none,您没有在视图中提及 cart_items。如果它从外部没有任何价值,则需要将其删除。或者在您的视图中为其提供动态数据。这就是为什么您的购物车模板现在 none 没有显示任何数据。