django学习-11.开发一个简单的醉得意菜单和人均支付金额查询页面
1.前言
刚好最近跟技术部门的【产品人员+UI人员+测试人员】,组成了一桌可以去公司楼下醉得意餐厅吃饭的小team。
所以为了实现这些主要点餐功能:
- 提高每天中午点餐效率,把点餐时间由20分钟优化为1分钟;
- 知道在哪些付款金额范围内,可以有哪些菜单可以选择;
- 知道人均付款金额;
- 知道微信需要付款总金额;
- 给餐厅老板发的点餐文案;
- 当前点餐菜单里的具体菜品名和价格;
结合之前学习的知识点,利用django框架的MTV思想,开发一个简单的html页面,让公司同个局域网的同事自己进行操作和查询醉得意菜单并得到想要的数据。
2.相关完整流程的操作步骤
2.1.第一步,在django项目【helloworld】的根路径【helloworld/】下,手动新增一个空文件【utils】,并把自己写的一个py脚本放到该文件【utils】里面。
细节:
- 工具类默认要放在同个文件夹A,文件夹A由使用人员手动创建;
- 文件夹A名称默认为utils,默认放在项目根目录;(大多数框架和行业人员使用时的默认遵守规范);
2.2.第二步,单元测试【zuideyiMenu.py】里的类【Zuideyi】代码功能,确保能正常实现。
# coding:utf-8
'''
@file: zuideyiMenu.py
@author: jingsheng hong
@ide: PyCharm
@createTime: 2020年12月31日 10点01分
@contactInformation: 727803257@qq.com
''' import random class Zuideyi:
'''醉得意,你懂的。''' def __init__(self,total,couponMoney,everyPlanMinimalMoney,everyPlanMaximalMoney):
'''
:param total 就餐人数
:param couponMoney 优惠券金额
:param everyPlanMinimalMoney 经过小伙伴协商达成的人均最低消费金额
:param everyPlanMaximalMoney 经过小伙伴协商达成的人均最高消费金额
''' self.total = total
self.couponMoney = couponMoney
self.everyPlanMinimalMoney = everyPlanMinimalMoney
self.everyPlanMaximalMoney = everyPlanMaximalMoney
# 自助费= 5元/人 (该属性值暂时固定)
self.supportFee = 5
# 折扣率 = 实际充值金额/最终获得金额 (该属性值暂时固定)
self.discountRate = 1000/1120
# 菜品集合(该属性值暂时固定)
self.menu = {
"招牌菜": {"酱椒片片鱼":68},
"6大必点菜": {"鲜花椒牛肉粒":58,"梅菜扣肉":46, "香酥脆皮鸭":32, "汽锅时蔬":28, "台式三杯鸡":42},
"醉经典": {"得意醉排骨":12,"肉末茄子":23, "铁板黑椒牛肉":48, "大碗有机花菜":22,"肉沫蒸蛋":18, },
"下饭南方菜": {"农家烧笋干":32,"海味紫菜煲":32,"干锅千叶豆腐":23,"红烧日本豆腐":23, },
"当季时蔬": {"油淋芥兰":18,},
"店长推荐": {"闽南醋肉":36,},
} # 大菜集合
# self.bigDish = self.menus()[0]
# 小菜集合
# self.sideDish = self.menus()[1] # # 新增一个空dict,用于存储相关需要展示到前端页面的打印信息
# self.printAll = {} def total_price_range_of_dishes(self):
'''
:return 可以点的菜品总价区间
'''
total_price_range_of_dishes = [self.everyPlanMinimalMoney*self.total/self.discountRate + self.couponMoney, self.everyPlanMaximalMoney*self.total/self.discountRate + self.couponMoney] return total_price_range_of_dishes def menus(self): '''
:return 返回一个整理过的菜品集合
'''
# 定义单价大于等于30元的菜品为【大菜】,定义单价小于30元的菜品为【小菜】
# 大菜数据集合:bigDish,小菜数据集合:sideDish
bigDish = []
sideDish = []
for key1,value1 in self.menu.items():
for key2,value2 in value1.items():
if value2>=30:
bigDish.append({key2:value2})
else:
sideDish.append({key2:value2})
dish = [bigDish,sideDish]
print("大菜集合如下:")
print(bigDish)
print("小菜集合如下:")
print(sideDish)
return dish def amount_of_payment_of_everyOne(self,totalMoney):
'''
:param totalMoney 醉得意点餐菜单总金额(不扣除相关优惠券金额,但包含就餐人数的总自助费)
:return 实际人均需付餐费
'''
amount_of_payment_zuiDeyi = (totalMoney-self.couponMoney)
print("今天醉得意公众号里的会员卡扣款金额:%s"%amount_of_payment_zuiDeyi) amount_of_payment_weChat = (totalMoney-self.couponMoney)*self.discountRate
print("今天微信实际支付扣款金额:%s"%amount_of_payment_weChat)
amount_of_payment_of_everyOne = (totalMoney-self.couponMoney)*self.discountRate/self.total
print("今天醉得意可以点的菜品总金额为%s元,点餐人数为:%s人,实际人均需付餐费:%s元" %(totalMoney,self.total,amount_of_payment_of_everyOne)) result1= {}
result1["今天醉得意公众号里的会员卡扣款金额:"] = amount_of_payment_zuiDeyi
result1["今天微信实际支付扣款金额:"] = amount_of_payment_weChat
result1["今天醉得意可以点的菜品总金额为:"] = totalMoney
result1["今天醉得意点餐人数为:"] = self.total
result1["今天醉得意实际人均需付餐费为:"] = amount_of_payment_of_everyOne
return result1 def chioce(self,bigDishCounts,sideDishCounts):
'''
:param bigDishCounts 大菜个数
:param sideDishCounts 小菜个数
'''
print("hongjingsheng")
dish = self.menus()
a1 = random.sample(dish[0],bigDishCounts)
b1 = random.sample(dish[1],sideDishCounts)
neededMenu =a1+b1 # 可以点的点餐菜品
totalMoney = 0 # 可以点的菜品总金额,初始值为0
stringA = "" # 在微信里要写给醉得意老板的菜单
for value3 in neededMenu:
for key4,value4 in value3.items():
# print(value4)
totalMoney = totalMoney + value4
stringA = stringA + key4 + ","
stringA=stringA[:-1]
# 总菜单金额 = 总菜品金额+ 总自助费
totalMoney = totalMoney + self.supportFee*self.total
# # 判断totalMoney 是否在可消费总金额区间 if totalMoney >= self.total_price_range_of_dishes()[0] and totalMoney <= self.total_price_range_of_dishes()[1]: result = {}
result["今天醉得意可以点的点餐菜品:"] = neededMenu
result["今天醉得意可以点的点餐菜品总数:"] = bigDishCounts+sideDishCounts
result["今天可以微信里发给醉得意老板的点餐文案:--->老板,今天点餐人数%s,11:45可以陆续上菜,桌上饮料杯子不用放,餐桌号安排好了之后麻烦说一声。点菜菜单如下:"%self.total] = stringA
result["今天总消费金额区间:"] = self.total_price_range_of_dishes() result1 = self.amount_of_payment_of_everyOne(totalMoney)
print("=================================================================================================================================================================") newResult = dict(result,**result1)
return newResult else:
return {"error:":"该场景下没有符合要求的醉得意菜单"} def run(self):
# 当11个人:3个大菜,3个小菜;3个大菜,4个小菜; 3个大菜,5个小菜;3个大菜,6个小菜;3个大菜,7个小菜; 4个大菜,3个小菜;4个大菜,4个小菜; 4个大菜,5个小菜;4个大菜,6个小菜;
# 当10个人:3个大菜,3个小菜;3个大菜,4个小菜; 3个大菜,5个小菜;3个大菜,6个小菜;3个大菜,7个小菜; 4个大菜,3个小菜;4个大菜,4个小菜; 4个大菜,5个小菜;4个大菜,6个小菜;
# 当9个人:3个大菜,3个小菜;3个大菜,4个小菜; 3个大菜,5个小菜;3个大菜,6个小菜; 2个大菜,4个小菜;2个大菜,5个小菜;2个大菜,6个小菜;2个大菜,7个小菜;
# 当8个人:3个大菜,2个小菜;3个大菜,3个小菜;3个大菜,4个小菜; 3个大菜,5个小菜;3个大菜,6个小菜; 2个大菜,3个小菜;2个大菜,4个小菜;2个大菜,5个小菜;2个大菜,6个小菜;
# 当7个人:2个大菜,3个小菜;2个大菜,4个小菜;2个大菜,5个小菜;2个大菜,6个小菜;
# 当6个人:2个大菜,3个小菜;2个大菜,2个小菜;2个大菜,1个小菜; 1个大菜,5个小菜;1个大菜,4个小菜;1个大菜,3个小菜;1个大菜,2个小菜;1个大菜,1个小菜;
if self.total == 10 or 11 :
a1 = self.chioce(bigDishCounts=3,sideDishCounts=3)
a2 = self.chioce(bigDishCounts=3,sideDishCounts=4)
a3 = self.chioce(bigDishCounts=3,sideDishCounts=5)
a4 = self.chioce(bigDishCounts=3,sideDishCounts=6)
a5 = self.chioce(bigDishCounts=3,sideDishCounts=7)
a6 = self.chioce(bigDishCounts=4,sideDishCounts=3)
a7 = self.chioce(bigDishCounts=4,sideDishCounts=4)
a8 = self.chioce(bigDishCounts=4,sideDishCounts=5)
a9 = self.chioce(bigDishCounts=4,sideDishCounts=6)
b = [a1,a2,a3,a4,a5,a6,a7,a8,a9]
return {"all":b} if self.total == 9 :
a1 = self.chioce(bigDishCounts=3,sideDishCounts=3)
a2 = self.chioce(bigDishCounts=3,sideDishCounts=4)
a3 = self.chioce(bigDishCounts=3,sideDishCounts=5)
a4 = self.chioce(bigDishCounts=3,sideDishCounts=6)
a5 = self.chioce(bigDishCounts=2,sideDishCounts=4)
a6 = self.chioce(bigDishCounts=2,sideDishCounts=5)
a7 = self.chioce(bigDishCounts=2,sideDishCounts=6)
a8 = self.chioce(bigDishCounts=2,sideDishCounts=7)
b = [a1,a2,a3,a4,a5,a6,a7,a8]
return {"all":b}
if self.total == 8 :
a1 = self.chioce(bigDishCounts=3,sideDishCounts=2)
a2 = self.chioce(bigDishCounts=3,sideDishCounts=3)
a3 = self.chioce(bigDishCounts=3,sideDishCounts=4)
a4 = self.chioce(bigDishCounts=3,sideDishCounts=5)
a5 = self.chioce(bigDishCounts=3,sideDishCounts=6)
a6 = self.chioce(bigDishCounts=2,sideDishCounts=3)
a7 = self.chioce(bigDishCounts=2,sideDishCounts=4)
a8 = self.chioce(bigDishCounts=2,sideDishCounts=5)
a9 = self.chioce(bigDishCounts=2,sideDishCounts=6)
a10 = self.chioce(bigDishCounts=2,sideDishCounts=7)
b = [a1,a2,a3,a4,a5,a6,a7,a8,a9,a10]
return {"all":b}
if self.total == 7 :
a1 = self.chioce(bigDishCounts=2,sideDishCounts=3)
a2 = self.chioce(bigDishCounts=2,sideDishCounts=4)
a3 = self.chioce(bigDishCounts=2,sideDishCounts=5)
a4 = self.chioce(bigDishCounts=2,sideDishCounts=6)
b = [a1,a2,a3,a4]
return {"all":b} if self.total == 6 :
a1 = self.chioce(bigDishCounts=2,sideDishCounts=3)
a2 = self.chioce(bigDishCounts=2,sideDishCounts=2)
a3 = self.chioce(bigDishCounts=2,sideDishCounts=1)
a4 = self.chioce(bigDishCounts=1,sideDishCounts=5)
a5 = self.chioce(bigDishCounts=1,sideDishCounts=4)
a6 = self.chioce(bigDishCounts=1,sideDishCounts=3)
a7 = self.chioce(bigDishCounts=1,sideDishCounts=2)
a8 = self.chioce(bigDishCounts=1,sideDishCounts=1)
b = [a1,a2,a3,a4,a5,a6,a7,a8]
return {"all":b} if __name__ =="__main__":
test = Zuideyi(9,5,19,22.5)
test.run()
2.3.第三步,在指定应用【hello】里的目录【templates】里编写一个【search_form.html】,用于让使用人员输入并提交相关数据。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>醉得意点餐页面</title>
</head>
<body>
<form action="{% url ‘search_interface’ %}" method="get">
请输入就餐的人数:<input type="text" name="total" placeholder="请输入就餐人数" value="7"/>
<br>
请输入优惠券金额:<input type="text" name="couponMoney" placeholder="请输入优惠券金额" value="5" />
<br>
人均最低消费金额:<input type="text" name="everyPlanMinimalMoney" placeholder="人均最低消费金额" value="22" />
<br>
人均最高消费金额:<input type="text" name="everyPlanMaximalMoney" placeholder="人均最高消费金额" value="30" />
<br>
<br>
<input type="submit" value="开始搜索相关合适的菜单信息"/>
</form>
</body>
</html>
细节:
①.form属性里的action元素的值的赋值含义,可以参考这篇博客:https://www.cnblogs.com/softidea/p/5426244.html
②.form属性里的action元素的值如何赋值的官方解释,可以参考这篇菜鸟教程:https://www.runoob.com/tags/att-form-action.html
③.action值的使用场景分析
2.4.第四步,在指定应用【hello】里的目录【templates】里编写一个【zuideyi_result.html】,用于展示查询到的醉得意菜单相关数据。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>符合点餐要求的醉得意</title>
</head>
<body>
{#<h4> {{views_dict}}</h4>#}
{#<h2> {{views_dict.b}}</h2>#}
{#<br>==============<br>#}
{##}
{##}
{#<h4> {{views_dict.all}}</h4>#}
{#<br>==============<br>#}
{##}
{##}
<ul>
<h4> {% for thing in views_dict.all %}
{% for i,j in thing.items %}
{{ i }}{{ j }}<br>
{% endfor %}
<br>
========================================
<br>
<br>
{% endfor %} </h4>
</ul>
</body>
</html>
2.5.第五步,在指定应用【hello】里的【views.py】里编写一个视图函数/接口【search_form】,用于返回【search_form.html】。
def search_form(request):
return render(request, 'search_form.html')
2.6.第六步,在指定应用【hello】里的【views.py】里编写一个视图函数/接口【search】,用于返回【zuideyi_result.html】。
from utils.zuideyiMenu import Zuideyi # 接收请求数据 def search(request):
request.encoding='utf-8'
# if 'total' in request.GET and request.GET['total']:
# message = '你搜索的内容为: ' + request.GET['total']
# else:
# message = '你提交了空表单' total = request.GET['total']
couponMoney = request.GET['couponMoney']
everyPlanMinimalMoney = request.GET['everyPlanMinimalMoney']
everyPlanMaximalMoney = request.GET['everyPlanMaximalMoney'] # list = [total,couponMoney,everyPlanMinimalMoney,everyPlanMaximalMoney] total = int(total)
couponMoney = int(couponMoney)
everyPlanMinimalMoney = float(everyPlanMinimalMoney)
everyPlanMaximalMoney = float(everyPlanMaximalMoney) zuideyi = Zuideyi(total=total,couponMoney=couponMoney,everyPlanMinimalMoney=everyPlanMinimalMoney,everyPlanMaximalMoney=everyPlanMaximalMoney)
run = zuideyi.run()
views_dict = run
return render(request,'zuideyi_result.html',{"views_dict":views_dict})
2.7.第七步,在django项目【helloworld】里路径为【helloworld/helloworld/urls.py/】里编写两个不同的url匹配规则。
url(r'^search-form/$', views.search_form),
url(r'^search/$', views.search),
2.8.第八步,启动django项目【helloworld】服务。
2.9.第九步,在任一浏览器输入地址【http://127.0.0.1:8000/search-form/】或者地址【http://个人电脑IP:8000/search-form/】,会成功访问到页面名为【醉得意点餐页面】的html页面。
2.10.第十步,在第九步得到的【醉得意点餐页面】里,点击按钮【开始搜索相关合适的菜单信息】,会成功访问到页面名为【符合点餐要求的醉得意】的html页面,该html页面会展示我们想要的相关内容。
django学习-11.开发一个简单的醉得意菜单和人均支付金额查询页面的更多相关文章
- 开发一个简单的postgresql extension
主要是学习如何编写一个简单的pg extension,参考https://severalnines.com/blog/creating-new-modules-using-postgresql-c ...
- LINUX内核分析第三周学习总结——构造一个简单的Linux系统MenuOS
LINUX内核分析第三周学习总结——构造一个简单的Linux系统MenuOS 张忻(原创作品转载请注明出处) <Linux内核分析>MOOC课程http://mooc.study.163. ...
- Python开发一个简单的BBS论坛
项目:开发一个简单的BBS论坛 需求: 整体参考“抽屉新热榜” + “虎嗅网” 实现不同论坛版块 帖子列表展示 帖子评论数.点赞数展示 在线用户展示 允许登录用户发贴.评论.点赞 允许上传文件 帖子可 ...
- 如何开发一个简单的HTML5 Canvas 小游戏
原文:How to make a simple HTML5 Canvas game 想要快速上手HTML5 Canvas小游戏开发?下面通过一个例子来进行手把手教学.(如果你怀疑我的资历, A Wiz ...
- 重新想象 Windows 8 Store Apps (64) - 后台任务: 开发一个简单的后台任务
[源码下载] 重新想象 Windows 8 Store Apps (64) - 后台任务: 开发一个简单的后台任务 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 后 ...
- Cocos2d-x-Lua 开发一个简单的游戏(记数字步进白色块状)
Cocos2d-x-Lua 开发一个简单的游戏(记数字步进白色块状) 本篇博客来给大家介绍怎样使用Lua这门语言来开发一个简单的小游戏-记数字踩白块. 游戏的流程是这种:在界面上生成5个数1~5字并显 ...
- Linux第三周学习总结——构造一个简单的Linux系统MenuOS
第三周学习总结--构造一个简单的Linux系统MenuOS 作者:刘浩晨 [原创作品转载请注明出处] <Linux内核分析>MOOC课程http://mooc.study.163.com/ ...
- 作业1开发一个简单的python计算器
开发一个简单的python计算器 实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568 ...
- 【Mac系统 + Python + Django】之开发一个发布会系统【Django视图(二)】
此学习资料是通过虫师的python接口自动化出的书学习而来的,在此说明一下,想学习更多的自动化的同学可以找虫师的博客园,非广告,因为我python+selenium自动化也是跟虫师学的,学习效果很好的 ...
随机推荐
- react+ant design 项目执行yarn run eject 命令后无法启动项目
如何将内建配置全部暴露? 使用create-react-app结合antd搭建的项目中,项目目录没有该项目所有的内建配置, 1.执行yarn run eject 执行该命令后,运行项目yarn sta ...
- Navicat,Dbeaver,heidiSql,DataGrip数据库连接工具比较
Navicat,Dbeaver,heidiSql,DataGrip数据库连接工具比较 1.Navicat 2.DBeaver 3.heidiSql 4.DataGrip 1.Navicat Navic ...
- CAS客户端和服务器配置https证书
关于如何生成https证书可以看这篇文章: java生成Https证书,及证书导入的步骤和过程 下面整理cas如何整合https: cas服务器端部署(TLS[https]) 1.生成证书: 参照ja ...
- Java 容器系列总结
为什么要使用集合 当我们需要保存一组类型相同的数据的时候,我们应该是用一个容器来保存,这个容器就是数组,但是,使用数组存储对象具有一定的弊端, 因为我们在实际开发中,存储的数据的类型是多种多样的,于是 ...
- Hadoop----hdfs dfs常用命令的使用
用法 -mkdir 创建目录 Usage:hdfs dfs -mkdir [-p] < paths> 选项:-p 很像Unix mkdir -p,沿路径创建父 ...
- 2013 Asia Hangzhou Regional Contest hdu4780 Candy Factory
参考:https://blog.csdn.net/sd_invol/article/details/15813671 要点 每个任务的结束时间是固定的,不受任何因素影响 机器只在最一开始有用,在那之后 ...
- Codeforces Round #683 (Div. 2, by Meet IT)【ABCD】
比赛链接:https://codeforces.com/contest/1447 A. Add Candies 题意 \(1\) 到 \(n\) 个袋子里依次有 \(1\) 到 \(n\) 个糖果,可 ...
- Codeforces 1355 E. Restorer Distance(三分)
传送门:E - Restorer Distance 题意:给出四个数 N, A, R, M ,然后给出一个长度为N的序列.让一个数+1花费A,-1花费R,从一个大的数向一个小的数移动1花费M.问让所 ...
- Best Reward && Girls' research
After an uphill battle, General Li won a great victory. Now the head of state decide to reward him w ...
- Filebeat 日志收集
Filebeat 介绍 Filebeat 安装 # 上传代码包 [root@redis03 ~]# rz filebeat-6.6.0-x86_64.rpm # 安装 [root@redis03 ~] ...