zoukankan      html  css  js  c++  java
  • flask路由(2)

    注册路由方式有三种。

    具体如下: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访问实现:

    文件上传:

  • 相关阅读:
    javascript基础全等号运算符
    javascript 使用ScriptX实现打印
    跨服务器与本地服务器不同数据库的SQL操作语句
    ASP.NET网络上实现单点登录
    FGMap API 帮助文档
    基于ArcEngine写的GoogleMap地图切割程序
    基于SuperMap Objects写的GoogleMap地图切割程序(三)
    使用SuperSocket开发联网斗地主(四):出牌
    JAVA创建对象方法
    Mysql 外键约束
  • 原文地址:https://www.cnblogs.com/topass123/p/13169033.html
Copyright © 2011-2022 走看看