经理管理一个餐厅,推出每天都有特色菜的营销模式。他想根据一周中的每一天有一种特色菜。
客人想知道当天的特色菜是什么。另外再添加一个介绍页面。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_templateapp = Flask(__name__)import jsonfrom datetime import datetimetoday = 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 biosowner = 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文件,显示特色菜
today is {
{ weekday }}, and special is { { specials }}
显示餐厅人员介绍
info The owner is a {
{ owner }}The cooker is a {
{ cooker }}The server is a {
{ server }}
关于flask的更多知识:http://flask.pocoo.org/docs