Before returning [flask] after the landing page redirect jump

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:

  1. Set session: through flask.sessioncan operate the session. Operating sessionjust like the operation is the same dictionary. session['username']='zhiliao'.
  2. Obtaining session: is similar session.get(key)dictionary .
  3. 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.
  1. 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

graph LR V (visit) -> A (Select Image page) A - not logged in -> B (landing page) B - successful landing -> A
graph LR V (visit) -> A (landing page) A - successful landing -> B (Home)

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') #登陆页面

Guess you like

Origin www.cnblogs.com/yywBlogW/p/11409648.html