Flask / Werkzeug,登录后如何返回上一页 [英] Flask/Werkzeug, how to return previous page after login
问题描述
在每个受限页面之前,都有一个装饰器来确保用户已经登录如果他们没有登录,现在返回到登录页面,如下所示:
#Decorator
def logged_in(f):
@wraps(f)
def deco_function(* args,** kwargs):
try:
如果不是session ['logged_in']:
flash('请先登录...','error')
return redirect(url_for('login'))
else:
return f(* args,** kwargs)
除了KeyError:
flash('请先登录...','error')
返回重定向(url_for('login'))
return decorated_function
$ b#登录函数
@ app.route('/',methods = ['GET','POST'])
def login():
登录页面。
if r equest.method =='POST':
###检查数据库等###
return render_template('login.jinja2')
#示例'restricted'页面
@ app.route('/ download_file')
@logged_in
def download_file():
用于下载文件的函数。
fileid = request.args.get('id',0)
### ... ###
登录后,需要将用户返回到将其带到登录页面的页面。
还需要保留诸如传递的变量之类的东西(例如整个链接基本上是www.example.com/download_file?id=3)
有没有人知道如何做到这一点?
感谢您的帮助: -
我认为标准做法是将成功登录后用户需要重定向的URL附加到登录URL的查询字符串的末尾。
你会改变你的装饰器到这样的东西(你的装饰器函数中的冗余也被移除了): 您将不得不substi请为 然后,您的登录处理程序,当用户成功登录时,检查请求中是否存在 I am using the Flask micro-framework which is based on Werkzeug, which uses Python. Before each restricted page there is a decorator to ensure the user is logged in, currently returning them to the login page if they are not logged in, like so: After logging in, it needs to return users to the page that took them to the login page.
It also needs to retain things such as the passed variables (i.e. the entire link basically www.example.com/download_file?id=3 ) Does anyone know how to do this?
Thank you for your help :-) I think standard practice is to append the URL to which the user needs to be redirected after a successful login to the end of the login URL's querystring. You'd change your decorator to something like this (with redundancies in your decorator function also removed): You'll have to substitute something for Then, in your login handler, when the user successfully logs in, you check to see if there's a 这篇关于Flask / Werkzeug,登录后如何返回上一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
pre $ def logged_in(f):
@wrap(f)
def deco_function(* args,** kwargs):$ b $如果session.get('logged_in')不是None:
返回f(* args,** kwargs)
else:
flash('请先登录...','error')
next_url = get_current_url()#然而,这在Flask
login_url ='%s?next =%s'%(url_for('login'),next_url)
返回重定向(login_url)
return decorated_function
get_current_url()
指定一些东西,因为我不知道在Flask中是如何完成这个工作的。
next
参数,如果是,则将其重定向到该URL。否则,你将它们重定向到一个默认的URL(通常是 /
,我想)。# Decorator
def logged_in(f):
@wraps(f)
def decorated_function(*args, **kwargs):
try:
if not session['logged_in']:
flash('Please log in first...', 'error')
return redirect(url_for('login'))
else:
return f(*args, **kwargs)
except KeyError:
flash('Please log in first...', 'error')
return redirect(url_for('login'))
return decorated_function
# Login function
@app.route('/', methods=['GET', 'POST'])
def login():
"""Login page."""
if request.method=='POST':
### Checks database, etc. ###
return render_template('login.jinja2')
# Example 'restricted' page
@app.route('/download_file')
@logged_in
def download_file():
"""Function used to send files for download to user."""
fileid = request.args.get('id', 0)
### ... ###
def logged_in(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if session.get('logged_in') is not None:
return f(*args, **kwargs)
else:
flash('Please log in first...', 'error')
next_url = get_current_url() # However you do this in Flask
login_url = '%s?next=%s' % (url_for('login'), next_url)
return redirect(login_url)
return decorated_function
get_current_url()
, because I don't know how that's done in Flask.next
parameter in the request and, if so, you redirect them to that URL. Otherwise, you redirect them to some default URL (usually /
, I guess).