Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to register login project by Flask

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report