flask自动重启项目

https://www.cnblogs.com/shenji/p/14855501.html
配置文件
app.config.from_object('settings.ConfigBase')
路由
# 添加路由的两种方式
@app.route('/')
def hello_world():
return 'Hello World!'
def index():
return 'index'
app.add_url_rule('/index', view_func=index)
可叠加
@app.route('/')
@app.route('/index')
def index():
return 'index'
反向生成
from flask import Flask, url_for
app = Flask(__name__)
@app.route('/')
def hello_world():
print(url_for('hello_world')) # 别名默认就是函数名
print(url_for('n1'))
return 'Hello World!'
# 起别名
@app.route('/index', endpoint='n1')
def index():
return 'index'
if __name__ == '__main__':
app.run()
不起别名默认就是函数名
路由转换器
@app.route('/index/<int:nid>')
def index(nid):
print(nid)
return f'index--{nid}'
常用的有五种
自定义路由转换器
- 编写一个类,继承BaseConverter
from flask import Flask
from werkzeug.routing import BaseConverter
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 value
def to_url(self, value):
"""
使用url_for反向生成URL时,传递的参数经过该方法处理,返回的值用于生成URL中的参数
:param value:
:return:
"""
val = super(RegexConverter, self).to_url(value)
return val
app = Flask(__name__)
# 添加到flask中
app.url_map.converters['regex'] = RegexConverter
app.config.from_object('settings.ConfigBase')
@app.route('/')
def hello_world():
return 'Hello World!'
regex = r'\w+@\w+\.\w+$'
@app.route(f'/index/<regex("{regex}"):nid>')
def index(nid):
print(nid)
return f'index--{nid}'
if __name__ == '__main__':
app.run()
简单版本
from flask import Flask
from werkzeug.routing import BaseConverter
class RegexConverter(BaseConverter):
"""
自定义URL匹配正则表达式
"""
def __init__(self, map, regex):
super(RegexConverter, self).__init__(map)
self.regex = regex
app = Flask(__name__)
# 添加到flask中
app.url_map.converters['regex'] = RegexConverter
regex = r'\w+@\w+\.\w+$'
@app.route(f'/index/<regex("{regex}"):nid>')
def index(nid):
print(url_for('index', nid=nid))
return f'index--{nid}'
带参数的反向解析
regex = r'\w+@\w+\.\w+$'
@app.route(f'/index/<regex("{regex}"):nid>')
def index(nid):
print(url_for('index', nid=nid))
return f'index--{nid}'
app.route参数
重定向
from flask import Flask, redirect
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
# 第一种
@app.route('/index', redirect_to='/')
def index():
return 'index'
# 第二种
@app.route('/index')
def index():
return redirect('/')
子域名访问
修改hosts文件
127.0.0.1 www.yyds.com
127.0.0.1 email.yyds.com
127.0.0.1 buy.yyds.com
127.0.0.1 abc.yyds.com
127.0.0.1 news.yyds.com
实现访问不同子域名执行不同的视图函数
from flask import Flask, redirect
app = Flask(__name__)
app.config['SERVER_NAME'] = 'yyds.com'
# 127.0.0.1 www.yyds.com
# 127.0.0.1 email.yyds.com
# 127.0.0.1 buy.yyds.com
# 127.0.0.1 abc.yyds.com
# 127.0.0.1 news.yyds.com
@app.route('/', subdomain='www')
def www():
return 'www'
@app.route('/', subdomain='abc')
def abc():
return 'abc'
@app.route('/', subdomain='news')
def news():
return 'news'
if __name__ == '__main__':
app.run(debug=True, port=80)
也可以获取访问的子域名
@app.route('/', subdomain='<username>')
@app.route('/index', subdomain='<username>')
def index(username):
print(username)
return username
重点:
- url
- methods
- endpoint:别名,默认是函数名
- @app.route('/index/int:nid/')
- url_for: 反向解析