添加路由
from flask import Flask,render_template,redirect app = Flask(__name__) """ 1. 执行decorator=app.route('/index',methods=['GET','POST']) 2. @decorator - decorator(index) """ # 路由方式一(*): @app.route('/index',methods=['GET','POST']) def index(): return "Index" # 路由方式二: def order(): return 'Order' app.add_url_rule('/order',view_func=order) if __name__ == '__main__': app.run()
反向生成
from flask import Flask,render_template,redirect,url_for app = Flask(__name__) @app.route('/index',methods=['GET','POST'],endpoint='n1') def index(): v1 = url_for('n1') v2 = url_for('login') v3 = url_for('logout') print(v1,v2,v3) return "Index" @app.route('/login',methods=['GET','POST']) def login(): return "login" @app.route('/logout',methods=['GET','POST']) def logout(): return "logout" if __name__ == '__main__': app.run()
路由转换器
from flask import Flask,render_template,redirect,url_for app = Flask(__name__) @app.route('/index',methods=['GET','POST'],endpoint='n1') def index(): v1 = url_for('n1') v2 = url_for('login') v3 = url_for('logout') print(v1,v2,v3) return "Index" @app.route('/login',methods=['GET','POST']) def login(): return "login" @app.route('/logout',methods=['GET','POST']) def logout(): return "logout" if __name__ == '__main__': app.run()
flask默认转换
DEFAULT_CONVERTERS = { 'default': UnicodeConverter, 'string': UnicodeConverter, 'any': AnyConverter, 'path': PathConverter, 'int': IntegerConverter, 'float': FloatConverter, 'uuid': UUIDConverter, }
自定义转换器
from flask import Flask,render_template,redirect,url_for from werkzeug.routing import BaseConverter app = Flask(__name__) class RegexConverter(BaseConverter): """ 自定义URL匹配正则表达式 """ def __init__(self, map, regex): super(RegexConverter, self).__init__(map) self.regex = regex def to_python(self, value): """ 路由匹配时,匹配成功后传递给视图函数中参数的值 :param value: :return: """ return int(value) def to_url(self, value): """ 使用url_for反向生成URL时,传递的参数经过该方法处理,返回的值用于生成URL中的参数 :param value: :return: """ val = super(RegexConverter, self).to_url(value) return val app.url_map.converters['xxx'] = RegexConverter @app.route('/index/<xxx("d+"):nid>',methods=['GET','POST']) def index(nid): print(nid,type(nid)) v = url_for('index',nid=999) # /index/999 print(v) return "Index" if __name__ == '__main__': app.run()
其他参数
# endpoint="" 默认是函数名, 可以在app.route()的关键字参数中定义 # url_for("") 反向地址, 通过视图函数名, 或endpoint解析对应的URL # methods=[] 该视图函数能处理的请求方式, 默认是GET, 当重新定义了methods, 那么默认的GET也会被覆盖 # # defaults={} 给视图函数传递参数, 可以作为默认参数, 传了就必须的接 # strict_slashes=Bool 严格的使用"/", URL中没有"/", 访问时也不能有, URL中有"/", 你访问时没有, 会通过301进行永久重定向 # redirect_to="" 永久重定向 # # '/shopping/<int:year>/<string:month>' 路由中使用参数, 并可以转换参数的数据类型, 切记数字可以转字符串, 字符串不能转数字 from flask import Flask,render_template,redirect app = Flask(__name__) @app.route('/index',methods=['GET','POST'],redirect_to='/new') def index(): return "老功能" @app.route('/new',methods=['GET','POST']) def new(): return '新功能' if __name__ == '__main__': app.run()
子域名
from flask import Flask,render_template,redirect app = Flask(__name__) app.config['SERVER_NAME'] = 'oldboy.com:5000' @app.route("/dynamic", subdomain="<username>") def xxxxxxx(username): print(username) return 'xxxxx' if __name__ == '__main__': app.run()