用flask进行web开发

经理管理一个餐厅,推出每天都有特色菜的营销模式。他想根据一周中的每一天有一种特色菜。

客人想知道当天的特色菜是什么。另外再添加一个介绍页面。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>
View Code

显示餐厅人员介绍

<!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>
View Code

 

关于flask的更多知识:http://flask.pocoo.org/docs

转载于:https://www.cnblogs.com/Tinamei/p/6541340.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值