Get the App
SLTechnology News&Howtos  ›  Development  › 

How to register login project by Flask

Shulou Source: shulou.com Published: 2022-06-01 11:22:54 09月16日 Update

This article mainly introduces the relevant knowledge of "how to achieve registration and login project in Flask". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope that this article "how to achieve registration and login project in Flask" can help you solve the problem.

Profile design

/ templates/config.py

# Database connection configuration import pymysqlconn = pymysql.connect (host='192.XXX.XXX.XX', port=320xx, user='root', password='123456', database='test_XX')

Home page / templates/index.html

Hello, {{username}}, welcome to my little station to quit.

Login page / templates/login.html

{# #} login user login {% if message%} {{message}} {% endif%} registration

Registration page / templates/register.html

Registered user registers {% if message%} {{message}} {% endif%} login

Login check / model/check_login.py

From templates.config import conncur = conn.cursor () def is_null (username,password): if (username==''or password==''): return True else: return Falsedef is_existed (username,password): sql= "SELECT * FROM user WHERE username='% s' and password='% s'% (username Password) cur.execute (sql) result = cur.fetchall () if (len (result) = = 0): return False else: return Truedef exist_user (username): sql = "SELECT * FROM user WHERE username ='% s'"% (username) cur.execute (sql) result = cur.fetchall () if (len (result) = = 0): return False else: return True

Registration check / model/regist_login.py

From templates.config import conncur = conn.cursor () def add_user (username, password): # sql commands sql = "INSERT INTO user (username, password) VALUES ('% swords password% s')"% (username, password) # execute (sql) cur.execute (sql) # commit conn.commit () # changes to the database content, commit () conn.close ()

Finally edit the running file

App.py

From flask import Flask,render_templatefrom flask import redirectfrom flask import url_forfrom flask import requestfrom model.check_login import is_existed,exist_user,is_nullfrom model.check_regist import add_userapp = Flask (_ _ name__) @ app.route ('/') def index (): return redirect (url_for ('user_login')) @ app.route (' / user_login',methods= ['GET') 'POST']) def user_login (): if request.method=='POST': # the request sent by registration is POST request username = request.form [' username'] password = request.form ['password'] if is_null (username,password): login_massage = "warm reminder: account and password are required" return render_template (' login.html' Message=login_massage) elif is_existed (username, password): return render_template ('index.html', username=username) elif exist_user (username): login_massage = "hint: incorrect password Please enter the correct password "return render_template ('login.html', message=login_massage) else: login_massage =" the user "return render_template (' login.html', message=login_massage) return render_template ('login.html') @ app.route (" / regiser ", methods= [" GET ") 'POST']) def register (): if request.method =' POST': username = request.form ['username'] password = request.form [' password'] if is_null (username,password): login_massage = "warm reminder: account and password are required" return render_template ('register.html' Message=login_massage) elif exist_user (username): return redirect (url_for ('user_login')) else: add_user (request.form [' username'], request.form ['password']) return render_template (' index.html') Username=username) return render_template ('register.html') if _ _ name__== "_ _ main__": app.run () on "how to register the login project for Flask" ends here Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Login password project user knowledge prompt warmth content small station data database file industry account page configuration different practical Hello practical Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei NVidia Xiaomi Shulou Information Shulou Tech Info