Flask學習
阿新 • • 發佈:2017-05-18
oat -a dex 定義 abort http set req aid
-from flask import Flask +from flask import Flask,render_template -from flask import request -from flask import make_response -from flask import abort -from flask import redirect -# __name__參數決定程序的根目錄 app = Flask (__name__) -# @app.route (‘/‘) -# def hello_world(): -# return ‘Hello World!‘[email protected] (‘/<name>‘) +def index(name): + return render_template(‘index.html‘,name=name) -# @app.route (‘/user/<name>‘) # -# def user(name): -# return ‘<h1>Hello,%s!</h1>‘% name -# -# @app.route (‘/user/<int:id>‘) # 還可以類型定義/user/<int:id> float path類型-# def user_id(id): -# return ‘<h1>Hello,%s!</h1>‘% id [email protected] (‘/user/<name>‘) +def user(name): + return render_template(‘user.html‘,name=name) -# @app.route (‘/‘) # 請求上下文 -# def index(): -# user_agent = request.headers.get(‘User-Agent‘) -# return ‘your browser is %s‘ % user_agent- -# @app.route (‘/‘) # 請求響應,可接受第二個參數為狀態碼 -# def index(): -# return ‘bad request‘,400 - -# @app.route (‘/‘) -# def hello_world(): -# response =make_response(‘這個文檔帶著COOKIE!‘) -# response.set_cookie(‘answer‘,‘42‘) -# return response - -# @app.route (‘/‘) -# def hello_world(): -# return redirect(‘http://www.baidu.com‘) [email protected] (‘/user/<id>‘) -def hello_world(id): - #user = load_user(id) - user = False - if not user: - abort(404) - return ‘hello %s‘ % user - -from flask.ext.script import Manager # 通過pip install flask-script 啟用manager 啟動後解析命令行 -manager = Manager(app) if __name__ == ‘__main__‘: - manager.run () - -# if __name__ == ‘__main__‘: -# app.run (debug=True) + app.run (debug=True)
pip install flask-script
Flask學習