1 Preface
Recently I learned the flask web framework based on python, which can realize a simple login interface. Flask is a lightweight customizable micro-manufacturing framework, written in python. Its advantage is that it is flexible, portable and safe, and can complete a lightweight web page in a short time. Although Flask is "tiny", it is extremely scalable and free. It has no database and window verification tools used by default.
2 Libraries needed to implement functions
Install flask library
pip install flask |
---|
The submodules to be called are request, redirect, render_template, session.
The respective roles are:
request: the mechanism for processing the request,
The methods are:
request.method: Get the front-end submission request method
request.form: Get the value passed in the form form
reques.args: Get the parameters passed in the url
and many more.
redirect: redirect the page according to the route
render_template: find and return to the html page, the default folder is templates if you want to change
Is app=Flask(name, template_folder='xxx')
session: Verify login status
3 Python code:
from flask import Flask, request, redirect, render_template,session app = Flask(name) app.secret_key='QWERTYUIOP'#Encrypt user information @app.route('/login',methods=['GET',"POST" ])#Route default receiving request mode is POST, but all the requests required for login, so special statement. 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':#Here can be judged according to the user and password in the database, because it is the simplest login interface, the database is not very good, all useless. session['user_info']=user return redirect('/index') else: return render_template('login.html',msg='User name or password entered incorrectly') @app.route('/index') 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() |
---|
HTML code:
<! DOCTYPE html>log in |
---|
5 Show results
Because it mainly introduces the Flsak backend, the front-end HTML is the simplest way.
Figure 1 Run interface
Figure 2 Login interface
Figure 3 Login with correct password
Figure 4 Successful login
Figure 5 Wrong login
6 to sum up
After the initial recognition of the use of Flask, I will learn more, hoping to touch the more in-depth application of Flask and achieve more functions.
END
Intern Editor | Wang Nanlan
Editor | Wang Ziqiang
**The stronger the ability, the greater the responsibility. Seek truth from facts, rigorous and meticulous. **
** ——Where2go team**