该瓶文档显示:
add_url_rule(*args, **kwargs)
Connects a URL rule. Works exactly like the route() decorator.
If a view_func is provided it will be registered with the endpoint.
endpoint – the endpoint for the registered URL rule. Flask itself assumes the name of the view function as endpoint
Run Code Online (Sandbox Code Playgroud)
"终点"究竟是什么意思?
我正在使用Flask并使用before_request装饰器将有关请求的信息发送到分析系统.我现在正在尝试创建一个装饰器,以防止在几条路线上发送这些事件.
我遇到的问题是让我的装饰器在before_request信号被触发之前被调用.
def exclude_from_analytics(func):
@wraps(func)
def wrapped(*args, **kwargs):
print "Before decorated function"
return func(*args, exclude_from_analytics=True, **kwargs)
return wrapped
# ------------------------
@exclude_from_analytics
@app.route('/')
def index():
return make_response('..')
# ------------------------
@app.before_request
def analytics_view(*args, **kwargs):
if 'exclude_from_analytics' in kwargs and kwargs['exclude_from_analytics'] is True:
return
Run Code Online (Sandbox Code Playgroud) 我有一个Angular/Flask网络应用程序,我正在尝试创建一个管理页面,该页面将由某个网址访问,例如"/ admin_page".该页面将需要其他身份验证,并且需要发出比所有其他用户的超时短的会话超时.
但是,我的印象是所有会话都是从我的烧瓶应用程序中的相同变量生成的,我将其配置为:
app.permanent_session_lifetime = timedelta(seconds=int)
所以,我的问题是:有没有办法在不影响其他用户会话的超时时间的情况下更改某些用户的会话超时长度?
即如果在我的路由处理程序中/admin_page我暂时更改了值app.permanent_session_lifetime,创建了用户的会话,然后将变量恢复为原始值,那么先前创建的会话是否会更改其超时值?
我有一个Flask课:
class Likes(object):
def __init__(self, model, table_id):
self.model = model
self.table_id = table_id
if request.form["likes"] == 'like':
query = self.model.query.filter_by(id=table_id).first()
query.likes += 1
db.session.commit()
flash(u'Like =)) ' + query.title, 'info')
elif request.form["likes"] == 'dislike':
query = self.model.query.filter_by(id=table_id).first()
query.likes -= 1
db.session.commit()
flash(u"Don't like =(" + query.title, 'info')
Run Code Online (Sandbox Code Playgroud)
我希望每次用户发送POST请求时调用此类,但每次创建我的类的实例时,我都需要添加检查请求类型:
# ...
if request.method == 'POST':
Likes(Post, request.form["post_id"])
# ...
Run Code Online (Sandbox Code Playgroud)
如何改进我的课程并在其中添加此检查:
if request.method == 'POST':
# ...
Run Code Online (Sandbox Code Playgroud)
解决方案: 使用装饰器@ app.before_request
@app.before_request
def before_req():
if request.method == 'POST':
flash(u'Before request', 'success')
Run Code Online (Sandbox Code Playgroud)