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: [email protected] ''' 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="/search/" 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>
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頁面會展示我們想要的相關內容。