经理管理一个餐厅,推出每天都有特色菜的营销模式。他想根据一周中的每一天有一种特色菜。
客人想知道当天的特色菜是什么。另外再添加一个介绍页面。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