Before returning after landing page redirect jump
I. Introduction
Jump to realize Forced landing page, features page before the return after landing. Many online jump landing page; how much function returns a page not before. Here I just used his method, there are drawbacks and other methods also please advice! ('Ε `)
二、session
1, Session concept
Session is located on the server side, similar to the Session structures to store user data, when the browser sends a request for the first time, a server automatically generates a Session and Session ID to uniquely identify the Session, and sends it to the response browser. When the second transmission request browser, a server will respond before the Session ID in the request sent together to the server, the server extracts the Session ID from the request, and all the Session ID and saved compared to find this user corresponding Session.
2, flask operation session:
- Set session: through
flask.session
can operate the session. Operatingsession
just like the operation is the same dictionary.session['username']='zhiliao'
. - Obtaining session: is similar
session.get(key)
dictionary . - Delete the value in the session: is similar to the dictionary. There are three ways you can delete the value in the session.
session.pop(key)
。del session[key]
。session.clear()
: All values in the deleted session.
- Set session of validity: If no session expiration date. Then the default is to expire after the browser is closed. If session.permanent = True, it will expire after 31 days in default. If you do not want to expire in 31 days, you can set
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(hour=2)
expire after two hours
Third, the design ideas
1, Process
2, the program
- @ Selected picture routing `
@user.route('/select_img/',methods=['GET'])
def select_img():
res = session.get('username')
if res == None: # 查到没有登陆
session['redirect'] = request.path # 保存重定向前的路由
return redirect(url_for('user.login')) # 跳转登陆
# 省略其余逻辑
return render_template('select_img.html') # 有登陆的话正常进入页面
- @ Routing landing
@user.route('/login/',methods=['GET','POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
user = User.query.filter(User.name == username).first()
if user and user.name == username and user.password == password:
# 登陆成功
session['username'] = user.name
res = session.get('redirect')
if res:
return redirect(res) # 重定向前的路由
return redirect('/') # 没有路由则跳转主页
return render_template('page-login.html') #登陆页面