濮阳杆衣贸易有限公司

主頁 > 知識庫 > Flask登錄注冊項目的簡單實現(xiàn)

Flask登錄注冊項目的簡單實現(xiàn)

熱門標簽:白銀外呼系統(tǒng) 唐山智能外呼系統(tǒng)一般多少錢 激戰(zhàn)2地圖標注 廣告地圖標注app 陜西金融外呼系統(tǒng) 哈爾濱ai外呼系統(tǒng)定制 公司電話機器人 騰訊外呼線路 海南400電話如何申請

本文主要介紹了Flask登錄注冊項目的簡單實現(xiàn),分享給大家,具體如下:

目錄結(jié)構(gòu)


配置文件設計
/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>
    h1>您好,{{ username }},歡迎來到我的小站/h1>
        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>
    h1>用戶登錄/h1>
    !--將登陸信息放到一個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>
    h1>用戶注冊/h1>
    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)的文章就介紹到這了,更多相關(guān)Flask登錄注冊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Python的Flask框架中實現(xiàn)簡單的登錄功能的教程
  • Flask框架通過Flask_login實現(xiàn)用戶登錄功能示例
  • 使用Python的Flask框架表單插件Flask-WTF實現(xiàn)Web登錄驗證
  • flask使用session保存登錄狀態(tài)及攔截未登錄請求代碼
  • Flask框架的學習指南之用戶登錄管理
  • python之Flask實現(xiàn)簡單登錄功能的示例代碼
  • python+flask編寫一個簡單的登錄接口

標簽:常德 黔西 鷹潭 黑龍江 惠州 益陽 上海 四川

巨人網(wǎng)絡通訊聲明:本文標題《Flask登錄注冊項目的簡單實現(xiàn)》,本文關(guān)鍵詞  Flask,登錄,注冊,項,目的,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Flask登錄注冊項目的簡單實現(xiàn)》相關(guān)的同類信息!
  • 本頁收集關(guān)于Flask登錄注冊項目的簡單實現(xiàn)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    星座| 辉南县| 伽师县| 铜山县| 乌拉特后旗| 淅川县| 无锡市| 英吉沙县| 龙陵县| 泰兴市| 石泉县| 大关县| 遂川县| 榆林市| 肥西县| 綦江县| 东城区| 上饶市| 北流市| 九寨沟县| 林芝县| 威信县| 洛扎县| 东兴市| 独山县| 丹巴县| 赤壁市| 信阳市| 保定市| 府谷县| 嵊泗县| 平陆县| 吴川市| 临清市| 乌鲁木齐县| 巍山| 吉林省| 铜陵市| 湄潭县| 聂荣县| 密云县|