python-Web-django-商城-不登陆添加购物车
utils:
# 商品多级联动
def get_category(categorys)->dict:
'''
:param:商品类性
:return: {{[],[]},{[],[]},{[],[]}}
'''
dict1 = forCategory(categorys)
return dict1 def forCategory(categorys):
'''递归'''
sum = {}
# if str(categorys) == "<QuerySet []>":
# return Goods.objects.filter(category=category)
for category in categorys:
new_category = Category.objects.filter(category_pid=category.id)
dict1 = forCategory(new_category)
if dict1 == {}:
dict1 = Goods.objects.filter(category=category)
sum[category] = dict1 return sum
'''个人虚拟购物车'''
import datetime
from app01.models import Cart class PersonCart():
'''个人虚拟购物车'''
def __init__(self):
self.cart = None
self.time = datetime.datetime.now() # 创建浏览者购物车
def getCart(self,goodsId,cart_num,cartIdList:list)->list:
self.cart = Cart(goods_id=goodsId,member_id=16,cart_num=cart_num)
self.cart.save()
cartIdList.append(self.cart.id)
return cartIdList # 删除浏览者购物车
@staticmethod
def delCart(cartIdList:list):
''''''
for cartId in cartIdList:
Cart.objects.filter(id=cartId).delete() # 浏览者购物添加到用户购物车里
@staticmethod
def addCart(cartIdList:list,memberId):
if cartIdList is not None:
for cartId in cartIdList:
Cart.objects.filter(id=cartId).update(member_id=memberId)
views:
from django.shortcuts import render,HttpResponse,redirect
from app01.models import *
from app01.utils.function import get_category
import json
from app01.utils.personCart import PersonCart
from app01.utils.personRedis import Redis def index(request):
#获取用户id,用户名
# member_session_id=request.session.get('memberId')
#
# member_session_name=request.session.get('memberName')
from app01.utils.personSession import PersonSession
person = PersonSession(request)
member_session_id = person.session.get('memberId')
member_session_name = person.session.get('memberName') if (request.COOKIES is not None) and (member_session_id is not None):
# 检查是否以前有浏览者登陆
checkPerson(request,member_session_id)
if member_session_id is None:
member_session_id = 16 # 拿取商品列表(缓存中)
redis = Redis() goods1 = redis.get('goods1')
if goods1 is None:
goods1=Goods.objects.filter(category__category_pid=1)[:4]
redis.set('goods1', goods1)
goods2 = redis.get('goods2')
if goods2 is None:
goods2 = Goods.objects.filter(category__category_pid=2)[:4]
redis.set('goods2', goods2)
goods3 = redis.get('goods3')
if goods1 is None:
goods3 = Goods.objects.filter(category__category_pid=3)[:4]
redis.set('goods3', goods3)
goods4 = redis.get('goods4')
if goods4 is None:
goods4 = Goods.objects.filter(category__category_pid=4)[:4]
redis.set('goods4', goods4)
goods5 = redis.get('goods5')
if goods1 is None:
goods5 = Goods.objects.filter(category__category_pid=5)[:4]
redis.set('goods5', goods5)
goods6 = redis.get('goods6')
if goods6 is None:
goods6 = Goods.objects.filter(category__category_pid=6)[:4]
redis.set('goods6',goods6) # 拿取商品种类
category1=Category.objects.filter(category_name='新鲜水果').first()
category2=Category.objects.filter(category_name='海产水产').first()
category3=Category.objects.filter(category_name='猪牛羊肉').first()
category4=Category.objects.filter(category_name='禽类蛋类').first()
category5=Category.objects.filter(category_name='新鲜蔬菜').first()
category6=Category.objects.filter(category_name='速冻食品').first() #商品字典
GOODSS = {
category1:goods1,
category2:goods2,
category3:goods3,
category4:goods4,
category5:goods5,
category6:goods6
} cart_num = 0
cart_nums = Cart.objects.filter(member_id=member_session_id)
for i in cart_nums:
cart_num += int(i.cart_num) # 商品多级联动
category_objs = Category.objects.filter(category_pid=0)
category_objs_san = get_category(category_objs) return render(request,'app01_index.html',locals()) # 检查浏览者登陆了
def checkPerson(request,memberId):
'''
:return:
'''
# 1 把浏览者购物添加到用户购物车里
# 2 并删除浏览者购物车
# 3 清道夫
cartIdList = request.COOKIES.get('cartIdList')
# 1 把浏览者购物添加到用户购物车里
PersonCart.addCart(cartIdList,memberId)
# 2 并删除浏览者购物车
rep = HttpResponse()
rep.delete_cookie('cartIdList')
# 3 清道夫
Cart.objects.filter(member_id=16).delete()
Cart.objects.filter(member_id=None).delete()
python-Web-django-商城-不登陆添加购物车的更多相关文章
- python web -- django
一. 安装 django $ pip install django (env)$ python >> import django >> django.VERSION >& ...
- python web——Django架构
环境:windows/linux/OS 需要的软件:Firefox 浏览器(别的也可以 不过firfox和python的webdriver兼容性好) git版本控制系统(使用前要配置 用户 编辑器可以 ...
- python web django base skill
web框架本质 socket + 业务逻辑 框架实现socket tonado node.js 使用WSGI实现socket django flask 自己实现框架思路 wsgiref socket ...
- python web django 2nd level -- 待更新
练习代码位置 实例代码位置 --> app: myblog Form 利用Form表单验证,自己写的html 思路: 新建一个类 LoginForm(forms.Form) 新建对象 obj = ...
- django 商城项目之购物车以及python中的一些redis命令
最近在用django restframe框架做一个商城项目,有一个关于购物车的业务逻辑,是用cookie和redis存储的购物车信息,在这里记录一下. 完成一个商城项目,如果不做一个购物车,就是十分可 ...
- [Python] 利用Django进行Web开发系列(一)
1 写在前面 在没有接触互联网这个行业的时候,我就一直很好奇网站是怎么构建的.现在虽然从事互联网相关的工作,但是也一直没有接触过Web开发之类的东西,但是兴趣终归还是要有的,而且是需要自己动手去实践的 ...
- [Python] 利用Django进行Web开发系列(二)
1 编写第一个静态页面——Hello world页面 在上一篇博客<[Python] 利用Django进行Web开发系列(一)>中,我们创建了自己的目录mysite. Step1:创建视图 ...
- python web框架——扩展Django&tornado
一 Django自定义分页 目的:自定义分页功能,并把它写成模块(注意其中涉及到的python基础知识) models.py文件 # Create your models here. class Us ...
- Redis & Python/Django 简单用户登陆
一.Redis key相关操作: 1.del key [key..] 删除一个或多个key,如果不存在则忽略 2.keys pattern keys模式匹配,符合glob风格通配符,glob风格的通配 ...
随机推荐
- 在Myeclipse中没有部署jeesite项目,但是每次运行其他项目时,还是会加载jeesite项目
解决办法: 一.在以下路径中找到jeesite文件,并删除 1.Tomcat 7.0\conf\Catalina\localhost 2.Tomcat 7.0\webapps 3.Tomcat 7.0 ...
- 开放式最短路径优先OSPF
1.OSPF基本知识 OSPF作为基于链路状态的协议,解决了RIP在收敛慢,路由环路,可扩展性差等问题,还有以下优点: 采用组播方式发布报文,可以减少对其他不运行ospf路由器的影响 ospf直尺无类 ...
- k8s-应用部署
该demo主要作为一个dubbo项目通过maven自动化docker打包插件发布到镜像仓库样例工程.该wiki后面同时会提供k8s部署zk,mysql,应用包的整个过程.该项目大体功能:zk作为注册中 ...
- ACM-ICPC 2017 南宁赛区现场赛 M. The Maximum Unreachable Node Set(二分图)
题目链接:https://nanti.jisuanke.com/t/19979 题意:给出一个 n 个点,m 条边的 DAG,选出最大的子集使得其中结点两两不能到达. 题解:参考自:https://b ...
- 洛谷P1026 统计单词个数【区间dp】
题目:https://www.luogu.org/problemnew/show/P1026 题意: 给定一个字符串,要求把他分成k段.给定s个单词,问划分成k段之后每段中包含的单词和最大是多少. 一 ...
- @GetMapping、@PostMapping、@PutMapping、@DeleteMapping、@PatchMapping、@RequestMapping详解
最近写项目中突然发现有人再controller层写@PostMapping,这对于经常用@RequestMapping的我来说,感到跟奇怪,网上搜寻了一些资料,特在此整合一下: Spring4.3中引 ...
- 05 django组件:contenttype
1.django组件:contenttype 组件的作用:可以通过两个字段让表和N张表创建FK关系 1.专题课,学位课 如何关联 过期时间?? 方法1:分别创建 专题课--过期时间表 .学位课--过期 ...
- 对JavaScript 模块化的深入-----------------引用
什么是模块化 好的代码模块分割的内容一定是很合理的,便于你增加减少或者修改功能,同时又不会影响整个系统. 为什么要使用模块 1.可维护性:根据定义,每个模块都是独立的.良好设计的模块会尽量与外部的 ...
- CPU内部结构图
原文地址:http://blog.csdn.net/jiuyueguang/article/details/9350793
- 7.20套娃(tao)
套娃(tao) input7 39 53 710 65 102 610 104 110 53 53 9output012 sol: 把查询想象成(x1,y1)向(x2,y2)有边当且仅当(x1< ...