flask USES session to save login status and intercept the code for non login requests

  • 2020-07-21 08:50:33
  • OfStack

This paper mainly studies flask's use of session to save login status and intercept the relevant content of non-login requests, which is detailed as follows.

Front-end request form:


<form action="/user/add" method="get"> 
    <input type="text" name="username" value="111"> 
    <input type="submit" value=" submit "> 
</form> 

When the front end submits, the back end receives the parameters and can save the login data in session:


@user.route('/add',methods=['GET']) 
def add(): 
  username=request.values.get('username'); 
  session['username']=username 
  return session['username'] 

Where you get the form data to use


request.values.get('username'); 

Check box parameters get:


s_option = request.values.getlist("s_option") 
for s in s_option: 
  pass 

Intercept the request for url using @before_ES20en


@user.before_request 
def before_user(): 
  if 'username' in session: 
    return ' Is logged in ' 
    pass 
 else: 
    return ' Not logged in ' 

If logged in, intercept, if not logged in, return a prompt message or jump to the login page

conclusion

That's all you need to know about flask's use of session to save login status and block the code for non-login requests. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: