溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Flask如何實現(xiàn)注冊登錄項目

發(fā)布時間:2022-03-01 10:28:58 來源:億速云 閱讀:111 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“Flask如何實現(xiàn)注冊登錄項目”的相關(guān)知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強(qiáng),希望這篇“Flask如何實現(xiàn)注冊登錄項目”文章能幫助大家解決問題。

配置文件設(shè)計
/templates/config.py

#數(shù)據(jù)庫連接配置
import pymysql

conn = pymysql.connect(
        host='192.XXX.XXX.XX',
        port=320xx,
        user='root',
        password='123456',
        database='test_XX'
    )

首頁/templates/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
{#    <link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}" rel="external nofollow"  rel="external nofollow" >#}
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet"  type="text/css" href="/static/style.css" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >
    <title>林家小豬測試小站</title>
</head>
<body>
    <div>
    <h2>您好,{{ username }},歡迎來到我的小站</h2>
        <a href="{{ url_for('user_login') }}" rel="external nofollow"  rel="external nofollow" >退出</a>
        <br/>
    </div>
</body>
</html>

登錄頁面/templates/login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet"  type="text/css" href="/static/style.css" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >
{#    <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}" rel="external nofollow"  rel="external nofollow"  type="text/css">#}
    <title>登錄</title>
</head>
<body>
    <div>
    <h2>用戶登錄</h2>
    <!--將登陸信息放到一個form中-->
    <form method="POST">
        <input type="text" name="username" placeholder="請輸入用戶名" />
        <br/>
        <input type="password" name="password" placeholder="請輸入密碼(小于12位)" />
        <br/>
         <!--jinja2的函數(shù)-->
        {% if message %} {{message}} {% endif %}
        <br/>
        <input type="submit" value="登錄" />
        <input type="reset" value="重置" />
        <!--跳轉(zhuǎn)到register的頁面-->
        <a href="{{ url_for('register') }}" rel="external nofollow" >注冊</a>
    </form>
    </div>
</body>
</html>

注冊頁面/templates/register.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet"  type="text/css" href="/static/style.css" rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >
    <title>注冊</title>
</head>
<body>
    <div>
    <h2>用戶注冊</h2>
    <form method="POST">
        <input type="text" name="username" placeholder="請輸入用戶名" />
        <br/>
        <input type="password" name="password" placeholder="請輸入密碼(小于12位)" />
        <br/>
        <!--jinja2的函數(shù)-->
        {% if message %} {{message}} {% endif %}
        <br/>
        <input type="submit" value="注冊" />
        <input type="reset" value="重置" />
        <a href="{{ url_for('user_login') }}" rel="external nofollow"  rel="external nofollow" >登錄</a>
    </form>
    </div>
</body>
</html>

登錄校驗 /model/check_login.py

from templates.config import conn
cur = conn.cursor()
def is_null(username,password):
	if(username==''or password==''):
		return True
	else:
		return False


def 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 True

def 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

注冊校驗 /model/regist_login.py

from templates.config import conn

cur = conn.cursor()

def add_user(username, password):
    # sql commands
    sql = "INSERT INTO user(username, password) VALUES ('%s','%s')" %(username, password)
    # execute(sql)
    cur.execute(sql)
    # commit
    conn.commit()  # 對數(shù)據(jù)庫內(nèi)容有改變,需要commit()
    conn.close()

最后編輯運行文件
app.py

from flask import Flask,render_template
from flask import redirect
from flask import url_for
from flask import request
from model.check_login import is_existed,exist_user,is_null
from model.check_regist import add_user

app = 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':  # 注冊發(fā)送的請求為POST請求
        username = request.form['username']
        password = request.form['password']
        if is_null(username,password):
            login_massage = "溫馨提示:賬號和密碼是必填"
            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 = "提示:密碼錯誤,請輸入正確密碼"
            return render_template('login.html', message=login_massage)
        else:
            login_massage = "不存在該用戶"
            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 = "溫馨提示:賬號和密碼是必填"
            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()

關(guān)于“Flask如何實現(xiàn)注冊登錄項目”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI