相信大家大学的时候都写过商城网站的作业,而商城类网站最复杂最困难的那就是购物车无疑了,今天我就给大家分享一个简单的Django购物车代码
1.中间页:
一个购物车肯定都应该有一个中间页的
(views.py):
def carJump(request, goods_id): #
# 1.先获取前端页面上提交过来的数据
user_id = request.COOKIES.get('user_id')
#print(user_id)
goods_obj = Goods.objects.get(id=int(user_id)) #获取数据库ID
count = request.POST.get('count') # 商品数量
goods_img_path = request.POST.get('goods_img') # 图片路径
# 2.保存数据,如果之前已经购买了此商品则数量增加,如果没有买就保存数据。
buy_car = BuyCar.objects.filter(goods_id=int(goods_id),user_id=int(user_id)).first() # 如果没有则返回None
# buy_car = BuyCar.objects.filter(goods_id=int(goods_id)).first() # 如果使用 索引,不存在报错
if buy_car:
"""如果数据库购物车表有此商品"""
buy_car.goods_num += int(count) # 当前数量增加, 注意 count 是字符串类型的。
else:
"""数据库中没有此商品"""
buy_car = BuyCar() #与数据库建立连接,将获取来的信息存入数据库
buy_car.goods_id = goods_id
buy_car.goods_num = int(count)
buy_car.goods_name = goods_obj.goods_name
buy_car.goods_price = goods_obj.goods_now_price
buy_car.goods_picture = goods_img_path
buy_car.user = Buyer.objects.get(id=request.COOKIES.get('user_id'))
buy_car.save() #保存
all_price = int(count) * float(buy_car.goods_price) #算出商品总价
return render(request, 'buyer/buyCar_jump.html', locals())
(html):
<div class="cart_list">
<div class="cart_top">{{ buy_car.goods_name }}已经成功添加到购物车</div>
<div class="cart_listbox">
<table width="100%" cellpadding="0" cellspacing="0" border='0px'>
<tr>
<th width='10%'>商品名称</th>
<th width='20%'>商品缩略图</th>
<th width='20%'>商品单价</th>
<th width='20%'>商品数量</th>
<th width='20%'>商品小计</th>
</tr>
<form method="post">
<tr>
<td><a href="#">{{ buy_car.goods_name }}</a></td>
<td><a href="#"><img src="{{ buy_car.goods_picture }}"/></a></td>
<td>¥{{ buy_car.goods_price }}元</td>
<td>{{ count }}</td>
<td>¥{{ all_price }}</td>
</form>
</tr>
</table>
<br>
<div>
<a class="btn" href="/buyer/goods_details/?id={{ goods_id }}">返回详情页</a>
{#需要判断当前用户是否登录#}
<a class="btn" href="/buyer/car_list/">去购物车结算</a>
<a class="btn" href="/buyer/add_address/">添加收货地址</a>
</div>
</div>
</div>
</div>
2.购物车页面:
(views.py):
def car_list(request):
# 将当前用户的所有数据查询出来。
user_id = request.COOKIES.get('user_id')
goods_lst = BuyCar.objects.filter(user=user_id) # 商品ID
new_goods_lst = [] # 设一个列表[{},{},{}]
for goods in goods_lst: #循环套取此客户ID下的购物车商品
all_price = float(goods.goods_price) * int(goods.goods_num) # 每一个商品的总价
new_goods_lst.append(
{'all_price': all_price, 'goods': goods} #添加到之前设的列表中,方便HTML页面套循环
)
# 查询当前用户所有的地址
address_lst = Address.objects.filter(buyer=user_id)
return render(request, 'buyer/car_list.html', locals())
#删除购物车中某一商品
def delete_goods(request, goods_id): #页面中点击删除goods_id商品ID号
user_id = request.COOKIES.get('user_id')
BuyCar.objects.get(goods_id=goods_id, user=int(user_id)).delete() #确认客户ID 和商品ID 删除
return redirect('/buyer/car_list/') #重定向此页面
# 清空购物车
def clear_goods(request):
user_id = request.COOKIES.get('user_id')
BuyCar.objects.filter(user=int(user_id)).delete() #删除此用户ID 下的所有商品
return redirect('/buyer/car_list/')
(html):
<form action="/buyer/enter_order/" method="post">
{% csrf_token %}
<div class="cart_listbox">
<table width="100%" cellpadding="0" cellspacing="0" border='0px'>
<thead>
<tr>
<th width='10%'>
<input type="checkbox" id="all"
style="margin-top: 20px; margin-left: 10px;width: 24px; height: 24px;">
</th>
<th width='10%'>商品名称</th>
<th width='20%'>商品缩略图</th>
<th width='20%'>商品单价</th>
<th width='20%'>商品数量</th>
<th width='20%'>商品小计</th>
<th width='10%'>操作</th>
</tr>
</thead>
<tbody id="j_tb">
{% if new_goods_lst %}
{% for data in new_goods_lst %}
<tr>
<td>{#name_1,name_2 #}
<input name="name_{{ data.goods.id }}" value="{{ data.goods.id }}" type="checkbox"
style="margin-left: 60px; width: 24px; height: 24px;">
</td>
{# 商品名称#}
<td><a href="#">{{ data.goods.goods_name }}</a></td>
{# 缩略图 #}
<td><a href="#"><img src="{{ data.goods.goods_picture }}"/></a></td>
{# 单价 #}
<td>¥{{ data.goods.goods_price }}元</td>
<td><input type="submit" name="-" value="-" class="btn1" formaction=""/>
{# 商品数量#}
<input type="text" value="{{ data.goods.goods_num }}" name="count" class="text1"/>
<input type="submit" name="+" value="+" class="btn1" formaction=""/></td>
{# 总价格#}
<td>¥{{ data.all_price }}</td>
<td><a href="/buyer/delete_goods/{{ data.goods.goods_id }}/">删除</a></td>
</tr>
{% endfor %}
{% else %}
<tr>
<td colspan="6" style="text-align: center;">购物车空空如也~~,请快去购物吧!</td>
</tr>
{% endif %}
</tbody>
</table>
</div>
<div class="shouhuo">
<div class="shouhuo_top">收货信息</div>
<label>收货地址:</label>
<select name="address" id="" class="input">
{% for add in address_lst %} #for 循环套取此ID下的收货地址
<option value="{{ add.id }}" class="input">{{ add.recver }}---{{ add.address }}</option>
{% endfor %}
</select>
<label>支付方式:</label>
<select name="pay_Method" id="" class="input">
<option value="zfb" class="input">支付宝</option>
<option value="wx" class="input">微信</option>
<option value="bank" class="input">银行卡</option>
</select>
</div>
<div class="goon">
<div class="clearcart">
<a href="/buyer/clear_goods/" class="btn">清空购物车</a>
</div>
<div class="totalprice">
总计100元
</div>
<div class="order">
<input type="submit" value="立即下单" class="btn"/>
</div>
</div>
</form>
中间的urls我就不写啦,各位加油!事业顺利!