首页 > Web开发 > 详细

用flask进行web开发

时间:2017-03-13 10:13:04      阅读:281      评论:0      收藏:0      [点我收藏+]

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

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

用flask进行web开发

原文:http://www.cnblogs.com/Tinamei/p/6541340.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!