注册路由方式有三种。

具体如下:app.route

from flask import Flask

app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Hello World!'
@app.route("/log/<user_name>")
def mylog(user_name):
    return "welcome to my home %s" %(user_name)


if __name__ == '__main__':
    app.run()

app.add_url_rule

from flask import Flask

app = Flask(__name__)


@app.route('/')
def hello_world():
    return 'Hello World!'
def log():
    return "welcome to my home"
#此处的/log/的最后一个“/”要写上。这使得他能够匹配有/或者无的两种情况
app.add_url_rule(rule="/log/",view_func=log) if __name__ == '__main__': app.run()

蓝图:访问http://127.0.0.1:5000/imooc/my/

from flask import Flask,Blueprint

app = Flask(__name__)
index_page
= Blueprint("index_page",__name__)
@index_page.route(
"/my/")
def index_page_index():
return "index page" app.register_blueprint(index_page,url_prefix = "/imooc")
@app.route(
'/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run()

结构分层处理【在大项目中使用】

 

 

# -*- coding: utf-8 -*-
from flask import Flask,Blueprint
'''
post/index 列表
post/info 详情
post/set 添加|编辑
post/ops 操作(删除|恢复)
'''

index_page = Blueprint( "index_page",__name__ )

@index_page.route( "/" )
def index_page_index():
    return "index page"


@index_page.route( "/me/" )
def hello():
    return "hello ,I Love Imooc"
from flask import Flask
from comm import index_page

app = Flask(__name__)

app.register_blueprint( index_page,url_prefix = "/imooc" )
if __name__ == "__main__":
    app.run()

post&get&upload文件处理

from flask import Flask
from comm import index_page

app = Flask(__name__)

app.register_blueprint( index_page,url_prefix = "/imooc" )
if __name__ == "__main__":
    app.run()
# -*- coding: utf-8 -*-
from flask import Flask,Blueprint,request

index_page = Blueprint( "index_page",__name__ )

@index_page.route( "/" )
def index_page_index():
    return "index page"

@index_page.route( "/me/" )
def hello():
    return "hello ,I Love Imooc"

@index_page.route("/get/")
def get():
    #获取request的值
    req = request.values
    #等同:var_a = request.args.get( "a","i love imooc" )
    var_a = req['a'] if "a" in req else "i love imooc"
    return "request:%s,params:%s,var_a:%s"%(request.method,request.args,var_a)

@index_page.route("/post/",methods = ['POST'])
def post():
    req = request.values
    var_a = req['a'] if "a" in req else "i love imooc"
    return "request:%s,params:%s,var_a:%s"%( request.method,request.args,var_a )

@index_page.route("/upload/",methods = [ "POST" ])
def upload():
    f = request.files['file'] if "file" in request.files else None
    return "request:%s,params:%s,file:%s"%(request.method,request.files,f )

 

get的访问方式:

 

 post访问实现:

 

 

文件上传:

 

posted on 2020-06-20 16:03  topass123  阅读(146)  评论(0编辑  收藏  举报