經理管理一個餐廳,推出每天都有特色菜的營銷模式。他想根據一周中的每一天有一種特色菜。
客人想知道當天的特色菜是什么。另外再添加一個介紹頁面。bios路徑下,顯示餐廳主人,廚師,服務生的簡介。
python文件同級目錄下創建templates,把所有模板都保存在這里。
廚師將當前特色菜品存儲在一個json文件中。
{"monday":"烘肉卷配辣椒醬",
"tuesday":"Hamburger",
"wednesday":"扣肉飯",
"thursday":"泡菜鍋",
"friday":"漢堡",
"saturday":"Pizza",
"sunday":"肥牛燒"}
把餐廳主任,廚師,服務生的介紹也保存在一個json文件中。
{"owner":"餐廳的主人",
"cooker":"帥帥的廚師",
"server":"美麗可愛漂亮大方的服務生"}
python代碼:datetime對象帶有weekday函數,返回數字(0,1,2……)代表星期幾
from flask import Flask
from flask import render_template
app = Flask(__name__)
import json
from datetime import datetime
today = datetime.now()
days_of_week = ('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday')
weekday = days_of_week[today.weekday()]
def get_specials():
try:
f = open("special_weekday.json")
specials = json.load(f)
f.close()
except IOError:
print "The file don\'t exist, Please double check!"
exit()
return specials[weekday]
@app.route('/')
def main_page():
return render_template('base.html', weekday = weekday, specials = get_specials())
def get_bios():
try:
f = open("info.json")
bios = json.load(f)
f.close()
except IOError:
print "The file don\'t exist, Please double check!"
exit()
return bios
owner = get_bios()['owner']
cooker = get_bios()['cooker']
server = get_bios()['server']
@app.route('/bios/')
def bios_page():
return render_template('bios.html', owner = owner, cooker = cooker, server = server)
if __name__ == '__main__':
app.run()
html文件,顯示特色菜
<!DOCTYPE html> <html> <head> <title></title> </head> <body> <p> today is {{ weekday }}, and special is {{ specials }} </p> </body> </html>
顯示餐廳人員介紹
<!DOCTYPE html> <html> <head> <title>info</title> </head> <body> <p>The owner is a {{ owner }}</p> <p>The cooker is a {{ cooker }}</p> <p>The server is a {{ server }}</p> </body> </html>
關於flask的更多知識:http://flask.pocoo.org/docs
