1.首先创建应用
-
将 templates和static移入到创建的应用中
2. init中的书写方式
from App.views import blue
from flask import Flask
def createApp():
app = Flask(__name__)
app.register_blueprint(blueprint=blue) # 还需将蓝图注册到app中
return app
3.app.py中的书写
from App import createApp
from flask_script import Manager
app = createApp()
manager = Manager(app)
if __name__="__main__"
manager.run()
4.views中路由的分发
from flask import Blueprint
blue = Blueprint('blue',__name__) # blue后面可以由于url反向解析
@blue.route('/hehe/')
def hehe()
return 'hehe'
@blue.route('/hello/')
def hello():
return redirect(url_for('blue.hehe'))
5.request获取参数
get请求获取参数:request.args.get('name') ,不存在返回None
get请求获取参数:request.args['name'] , 不存在会报错post请求获取参数:request.form.get('password'),不存在返回None
post请求获取参数:request.form['password'], 不存在会报错获取cookie:request.cookies.get('name')
获取上传文件:request.files.get('file')
6.重定向并且设置cookie
@blue.route('/test/')
def hello()
temp = redirect(url_for('blue.hello'))
response = make_response(temp) # 需要导入make_response
response.set_cookie('name',name)
return response
7.定制专门的错误界面
@blue.errorhandler(404)
def handler404(ex):
return render_template('404.html')
网友评论