1.可傳入?yún)?shù):
@app.route('/user/
') #常用的 不加參數(shù)的時(shí)候默認(rèn)是字符串形式的
@app.route('/post/
') #常用的 #指定int,說(shuō)明是整型的
@app.route('/post/
')
@app.route('/post/
')
@app.route('/login', methods=['GET', 'POST'])
DEFAULT_CONVERTERS = {
'default': UnicodeConverter,
'string': UnicodeConverter,
'any': AnyConverter,
'path': PathConverter,
'int': IntegerConverter,
'float': FloatConverter,
'uuid': UUIDConverter,
}
2.反向生成URL: url_for
endpoint("name") #別名,相當(dāng)于django中的name
from flask import Flask, url_for
'''
遇到不懂的問(wèn)題?Python學(xué)習(xí)交流群:821460695滿足你的需求,資料都已經(jīng)上傳群文件,可以自行下載!
'''
@app.route('/index',endpoint="xxx") #endpoint是別名
def index():
v = url_for("xxx")
print(v)
return "index"
@app.route('/zzz/
',endpoint="aaa") #endpoint是別名
def zzz(nid):
v = url_for("aaa",nid=nid)
print(v)
return "index2"
3. @app.route和app.add_url_rule參數(shù)
@app.route和app.add_url_rule參數(shù):
rule, URL規(guī)則
view_func, 視圖函數(shù)名稱
defaults=None, 默認(rèn)值,當(dāng)URL中無(wú)參數(shù),函數(shù)需要參數(shù)時(shí),使用defaults={'k':'v'}為函數(shù)提供參數(shù)
endpoint=None, 名稱,用于反向生成URL,即: url_for('名稱')
methods=None, 允許的請(qǐng)求方式,如:["GET","POST"]
strict_slashes=None, 對(duì)URL最后的 / 符號(hào)是否嚴(yán)格要求,
如:
@app.route('/index',strict_slashes=False), #當(dāng)為False時(shí),url上加不加斜杠都行
訪問(wèn) http://www.xx.com/index/ 或 http://www.xx.com/index均可
@app.route('/index',strict_slashes=True) #當(dāng)為True時(shí),url后面必須不加斜杠
僅訪問(wèn) http://www.xx.com/index
redirect_to=None, 由原地址直接重定向到指定地址,原url有參數(shù)時(shí),跳轉(zhuǎn)到的新url也得傳參,注意:新url中不用指定參數(shù)類型,直接用舊的參數(shù)的類型
如:
@app.route('/index/
', redirect_to='/home/
') # 訪問(wèn)index時(shí),會(huì)直接自動(dòng)跳轉(zhuǎn)到home,執(zhí)行home的函數(shù),
不執(zhí)行index的
或
def func(adapter, nid):
return "/home/888"
@app.route('/index/
', redirect_to=func)
subdomain=None, 子域名訪問(wèn)
from flask import Flask, views, url_for
app = Flask(import_name=__name__)
app.config['SERVER_NAME'] = 'haiyan.com:5000'
@app.route("/", subdomain="admin")
def static_index():
"""Flask supports static subdomains
This is available at static.your-domain.tld"""
return "admin.xxx.com"
#動(dòng)態(tài)生成
@app.route("/dynamic", subdomain="
")
def username_index(username):
"""Dynamic subdomains are also supported
Try going to user1.your-domain.tld/dynamic"""
return username + ".your-domain.tld"
if __name__ == '__main__':
app.run()
所有的域名都得與IP做一個(gè)域名解析:
如果你想通過(guò)域名去訪問(wèn),有兩種解決方式:
方式一:
1、租一個(gè)域名 haiyan.lalala
2、租一個(gè)公網(wǎng)IP 49.8.5.62
3、域名解析:
haiyan.com 49.8.5.62
4、吧代碼放在49.8.5.62這個(gè)服務(wù)器上,程序運(yùn)行起來(lái)
用戶可以通過(guò)IP進(jìn)行訪問(wèn)
方式二:如果是自己測(cè)試用的就可以用這種方式。先在自己本地的文件中找
C:\Windows\System32\drivers\etc 找到HOST,修改配置
然后吧域名修改成自己的本地服務(wù)器127.0.0.1
加上配置:app.config["SERVER_NAME"] = "haiyan.com:5000"
# =============== 子域名訪問(wèn)============
@app.route("/static_index", subdomain="admin")
def static_index():
return "admin.bjg.com"
# ===========動(dòng)態(tài)生成子域名===========
@app.route("/index",subdomain='
')
def index(xxxxx):
return "%s.bjg.com" %(xxxxx,)
4.自定制正則路由匹配
擴(kuò)展Flask的路由系統(tǒng),讓他支持正則,這個(gè)類必須這樣寫(xiě),必須去繼承BaseConverter
from flask import Flask,url_for
from werkzeug.routing import BaseConverter
'''
遇到不懂的問(wèn)題?Python學(xué)習(xí)交流群:821460695滿足你的需求,資料都已經(jīng)上傳群文件,可以自行下載!
'''
app = Flask(__name__)
# 定義轉(zhuǎn)換的類 class RegexConverter(BaseConverter):
"""
自定義URL匹配正則表達(dá)式
"""
def __init__(self, map, regex):
super(RegexConverter, self).__init__(map)
self.regex = regex
def to_python(self, value):
"""
路由匹配時(shí),匹配成功后傳遞給視圖函數(shù)中參數(shù)的值
:param value:
:return:
"""
return int(value)
def to_url(self, value):
"""
使用url_for反向生成URL時(shí),傳遞的參數(shù)經(jīng)過(guò)該方法處理,返回的值用于生成URL中的參數(shù)
:param value:
:return:
"""
val = super(RegexConverter, self).to_url(value)
return val
# 添加到converts中
app.url_map.converters['regex'] = RegexConverter
# 進(jìn)行使用
@app.route('/index/
',endpoint='xx')
def index(nid):
url_for('xx',nid=123)
return "Index"
if __name__ == '__main__':
app.run()
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
