Flask web development handles POST request implementation (login case)

  • 2020-11-18 06:19:51
  • OfStack

In this article, we illustrate Flask's handling of post requests with a login example.

1. Create application directory, such as


mkdir example
cd example

2. Create run. py file in the application directory with the following contents


from flask import Flask
from flask import render_template, redirect,url_for
from flask import request

app = Flask(__name__)

@app.route('/login', methods=['POST','GET'])
def login():
  error = None
  if request.method == 'POST':
    if request.form['username']=='admin':
      return redirect(url_for('home',username=request.form['username']))
    else:
      error = 'Invalid username/password'
  return render_template('login.html', error=error)

@app.route('/home')
def home():
  return render_template('home.html', username=request.args.get('username'))

if __name__ == '__main__':
  app.debug = True
  app.run('0.0.0.0',80)

The above code is explained as follows:

1) The above code USES several flask methods

render_template: Locate the request to the template file, process the template file, and return the result as a response to the request

redirect: Redirect the response to the request to the new url. The above example is a redirect to the home page after a successful login.

url_for: Generates url based on parameters

2) Use of request objects

The request object contains all the request information, which is used to obtain the required request information.

3) app.route adds the methods parameter, indicating the http request mode supported by the url. The default is get mode. The above example /login serves as both get and post's request target.

3. Create templates directory under the application directory and login. html and home. html under the templates directory, the contents are as follows:

1) login. html file


<!DOCTYPE html>
<html lang="zh-CN">
 <head>
  <meta charset="utf-8">
  <title>login</title>
 </head>
 <body>
  <form style="margin:20px;border:1px solid red" method="post" action="/login">
    <span>username:</span><input type="text" name="username" id="username"><br/>
    <span>password:</span><input type="password" name="password" id="password"><br/>
    <button type="submit" id="loginBtn">login</button>
  </form>
  {% if error %}
    <h1 style="color:red">{{ error }}!</h1>
  {% endif %}
 </body>
</html>

2) home html


<!DOCTYPE html>
<html lang="zh-CN">
 <head>
  <meta charset="utf-8">
  <title>home</title>
 </head>
 <body>
  <h1>wlcome {{username}} , this is home</h1>
 </body>
</html>

4. Start the service

Run python ES70en.py in the application directory

5. Test access

http://192.168.142.138/login

Note: after the success of the login, will enter http: / / 192.168.142.138 home & # 63; username = admin page

This url does not display well. You do not need to pass in username as session, but get it in ES88en.html via session.

This will be covered in a later article.


Related articles: