1 introduction
I’ve recently learned the Flask Web framework based on Python to implement a simple login interface. Flask is a lightweight, customizable miniaturization framework written in Python. Its advantage is flexible, light, safe, can complete a lightweight web page in a short time. Flask is “small” but extremely scalable and free, with no default database and window validation tools.
2 Libraries required to implement functions
Install the flask library
wxid_dtmbr8hu9r5j22
Copy the code
The submodules to call are request, Redirect, render_template, and session.
The respective functions are:
Request: a mechanism for processing requests,
Methods are:
Request. method: obtains the front-end request submission mode
Request. form: Gets the value passed in the form form
Reques.args: Gets the parameters passed in the URL
And so on.
Redirect: Redirects the page based on routes
Render_template: Finds and returns the HTML page. The default folder is templates if you want to change
App =Flask (name, template_folder=’ XXX ‘)
Session: Verifies the login status
3 Python code:
from flask import Flask, request, redirect, Render_template,session app = Flask(__name__) app. Secret_key ='QWERTYUIOP'# @app.route('/login',methods=['GET', 'POST'])# def login(): if request.method=='GET': return render_template('login.html') user=request.form.get('user') pwd=request.form.get('pwd') if user=='admin' and PWD =='123':# select * from user where username =='123'; session['user_info']=user return redirect('/index') else: Return render_template('login. HTML ', MSG =' @app.route ') def index(): render_template('login. HTML ', MSG =' @app.route ') def index(): user_info=session.get('user_info') if not user_info: return redirect('/login') return 'hello' @app.route('/logout') def logout_(): del session['user_info'] return redirect('login') if __name__ == "__main__": app.run()Copy the code
4 HTML code:
<! DOCTYPE HTML > < HTML > <head> <meta charset="UTF-8"> <title> </head> <body> <h1> </h1> <form method="post"> <input type="text" name="user" > <input type="password" name=" PWD "> < submit type=" name ">{{MSG}} </form> </body> </html>Copy the code
5 Effect Display
Because the main introduction of Flsak background, front-end HTML is the most simple way.
FIG. 1 Operating interface
Figure 2 Login interface
Figure 3 Login with the correct password
Figure 4 Successful login
Figure 5 Incorrect login
6 summarizes
After the initial understanding of Flask application, I will learn more deeply, hoping to touch more in-depth application of Flask and achieve more functions.