一、购物车功能
1、编写 shopper 中的 views.py
定义视图函数 shopcartView
:
from commodity.models import CommodityInfos
from .form import *
from .models import OrderInfos, CartInfos
@login_required(login_url='/shopper/login.html')
def shopcartView(request):
title = '我的购物车'
classContent = 'shopcarts'
id = request.GET.get('id', '')
quantity = request.GET.get('quantity', 1)
userID = request.user.id
if id:
CartInfos.objects.update_or_create(commodityInfos_id=id, user_id=userID, quantity=quantity)
return redirect('shopper:shopcart')
getUserId = CartInfos.objects.filter(user_id=userID)
commodityDcit = {
x.commodityInfos_id: x.quantity for x in getUserId}
commodityInfos = CommodityInfos.objects.filter(id__in=commodityDcit.keys())
return render(request, 'shopcart.html', locals())
2、编写shopcart.html
{
% extends 'base.html' %}
{
% load static %}
{
% block content %}
<div class="banner-bg w1200">
<h3>夏季清仓</h3>
<p>宝宝被子、宝宝衣服3折起</p>
</div>
<div class="cart w1200">
<div class="cart-table-th">
<div class="th th-chk">
<div class="select-all">
<div class="cart-checkbox">
<input class="check-all check" id="allCheckked" type="checkbox" value="true">
</div>
<label> 全选</label>
</div>
</div>
<div class="th th-item"></