最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python Flask基礎(chǔ)到登錄功能的實(shí)現(xiàn)代碼

 更新時(shí)間:2021年05月12日 10:00:43   作者:Scr1pt_kid  
這篇文章主要介紹了Python Flask基礎(chǔ)到登錄功能的實(shí)現(xiàn)代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

視頻教程教學(xué)地址:https://www.bilibili.com/video/BV18441117Hd?p=1

0x01路由

from flask import Flask

app = Flask(__name__)  # flask對(duì)象實(shí)例化
 
@app.route('/index')    #定義首頁
@app.route('/')       #設(shè)置默認(rèn)index
def index():
    return 'hello world!'

@app.route('/home/<string:username>')   # 生成home路由,單一傳參
def home(username):
    print(username)
    return '<h1>歡迎回家</h1>'

@app.route('/main/<string:username>/<string:password>') #多個(gè)參數(shù)傳遞
def main(username,password):
    print(username)
    print(password)
    return '<h1>welcome</h1>'

def about():
    return  'about page'
app.add_url_rule(rule='/about',view_func=about)  #另一種添加路由的方式

if __name__ == '__main__':
    app.debug = True  #開啟debug模式
    app.run()

0x02 模版和靜態(tài)文件

2.1 文件結(jié)構(gòu)

在這里插入圖片描述

2.2代碼

#app.py
#app.py
from flask import Flask,render_template    #倒入模版

app = Flask(__name__) #聲明模版文件夾


@app.route(('/index'))
def index():

  return render_template('index.html') #返回模版

if __name__ == '__main__':
    app.run(debug=True)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>hello hello</h1>
    <img src="/static/imgs/1.png">
</body>
</html>

2.3 運(yùn)行效果

在這里插入圖片描述

0x03 json

from flask import Flask,jsonify

app = Flask(__name__)

@app.route('/')
def index():
    user = {'name':'李三','password':'123'}
    return jsonify(user)

if __name__ == '__main__':
    app.run(debug=True)

3.1運(yùn)行效果

在這里插入圖片描述

0x04 重定向

4.1 訪問跳轉(zhuǎn)

from flask import Flask, redirect  #導(dǎo)入跳轉(zhuǎn)模塊

app = Flask(__name__)

@app.route('/index')
def index():
    return redirect('https://www.baidu.com') #指定跳轉(zhuǎn)路徑,訪問/index目錄即跳到百度首頁

@app.route('/home')
def home():
    return 'home page'
if __name__ == '__main__':
    app.run(debug=True)

4.2 打印路由

from flask import Flask,url_for #導(dǎo)入模塊

app = Flask(__name__)

@app.route('/index')
def index():
    return 'test'

@app.route('/home')
def home():
    print(url_for('index'))   打印 index路由
    return 'home page'

if __name__ == '__main__':
    app.run(debug=True)

4.3 跳轉(zhuǎn)傳參

# 訪問home,將name帶入index并顯示在頁面
from flask import Flask,url_for,redirect #導(dǎo)入模塊

app = Flask(__name__)

@app.route('/index<string:name>')
def index(name):
    return 'test %s' % name

@app.route('/home')
def home():
    return redirect(url_for('index',name='admin'))

if __name__ == '__main__':
    app.run(debug=True)

0x05 jinjia2模版

 5.1代碼

from flask import Flask,render_template    #倒入模版

app = Flask(__name__) #聲明模版文件夾


@app.route(('/index'))
def index():
    user = 'admin'
    data = ['111',2,'李三']
    userinfo = {'username':'lisan','password':'12333'}
    return render_template('index.html',user=user,data=data,userinfo=userinfo) #返回模版,傳入數(shù)據(jù)

if __name__ == '__main__':
    app.run(debug=True)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
  <h1>11111</h1>

  {{user}}
  {{data}}   #直接傳入

  {% if user == 'admin'%}   #簡(jiǎn)單邏輯判斷
    <h1 style="color:red">管理員</h1>
  {% else %}
    <h1 style="color:green">普通用戶</h1>
  {% endif %}
  <hr>

  {% for item in data %}   # for循環(huán)
    <li>{{item}}</li>
  {% endfor %}

  <hr>
  {{ userinfo['username'] }}   
  {{ userinfo['password'] }}

  <hr>
  {{ user | upper }}   #字母大寫(更多可查閱jinjia2過濾器)
</body>
</html>

5.2 運(yùn)行效果

在這里插入圖片描述

0x06 藍(lán)圖

目的是為了更好的細(xì)分功能模塊

6.1代碼結(jié)構(gòu)

├── admin
│   └── admin.py
└── app.py

6.2 代碼

#admin.py
from flask import Blueprint  導(dǎo)入藍(lán)圖模塊

admin = Blueprint('admin',__name__,url_prefix='/admin') #對(duì)象實(shí)例化,url_prefix添加路由前綴,表示若想訪問本頁相關(guān)路由,只能通過形如 xxx/admin/login 訪問,不能 xxx/login訪問

@admin.route('/register')
def register():
    return '歡迎注冊(cè)'

@admin.route('/login')
def login():
     return '歡迎登錄'
#app.py
from flask import Flask
from admin.admin import admin as admin_blueprint   # 導(dǎo)入藍(lán)圖


app = Flask(__name__) #聲明模版文件夾
app.register_blueprint(admin_blueprint)  #注冊(cè)藍(lán)圖

@app.route(('/index'))
def index():
    return 'index page'

if __name__ == '__main__':
    app.run(debug=True)

0x07 登錄

 7.1結(jié)構(gòu)

在這里插入圖片描述

7.2代碼

#web.py
from flask import Flask,render_template,request,redirect,flash,url_for,session
from os import urandom
app = Flask(__name__)
app.config['SECRET_KEY'] = urandom(50)
@app.route('/index')
def index():
    if not session.get('user'):
            flash('請(qǐng)登錄后操作','warning')
            return redirect(url_for('login'))
    return render_template('index.html')

@app.route('/login',methods=['GET','POST'])
def login():
    if request.method == 'GET':
        return  render_template('login.html')
    elif request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('password')

        if username == 'admin' and password == '888888':
            flash('登錄成功','success')
            session['user'] = 'admin'
            return redirect(url_for('index'))
        else:
            flash('登錄失敗','danger')
            return  redirect(url_for('login'))

if __name__ == '__main__':
    app.run(debug=True)
# index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet"  rel="external nofollow"  rel="external nofollow"  integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">

<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>

</head>
<body>
    <h1>歡迎你,管理員</h1>
     {% for color, message in get_flashed_messages(with_categories=True) %}
     <div class="alert alert-{{color}} alert-dismissible" role="alert">
  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
    <p>{{message}}</p>
</div>
  {% endfor %}
</body>
</html>
#login.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>login</title>
  <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet"  rel="external nofollow"  rel="external nofollow"  integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">

<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
</head>
<body>
  <form action="/login" method="post">
    <div class='form-group'>
      <input type="text" name="username" placeholder="請(qǐng)輸入用戶名" class="form-control">
    </div>
    <div class='form-group'>
      <input type="password" name="password" placeholder="請(qǐng)輸入密碼" class="form-control">
    </div>
    <div class="form-group">
      <input type="submit" value= "submit" class="btn btn-primary">
    </div>

  </form>

      {% for color, message in get_flashed_messages(with_categories=True) %}
     <div class="alert alert-{{color}} alert-dismissible" role="alert">
  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
    <p>{{message}}</p>
</div>
  {% endfor %}
</body>
</html>

7.3實(shí)現(xiàn)效果

7.3.1未登錄默認(rèn)跳轉(zhuǎn)到登錄頁面

在這里插入圖片描述

7.3.2登錄成功跳轉(zhuǎn)到index頁面

賬戶密碼:admin/888888

在這里插入圖片描述

7.3.2登錄失敗效果

在這里插入圖片描述

到此這篇關(guān)于Python Flask基礎(chǔ)到登錄功能的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Python Flask登錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • PyTorch手寫數(shù)字?jǐn)?shù)據(jù)集進(jìn)行多分類

    PyTorch手寫數(shù)字?jǐn)?shù)據(jù)集進(jìn)行多分類

    這篇文章主要介紹了PyTorch手寫數(shù)字?jǐn)?shù)據(jù)集進(jìn)行多分類,損失函數(shù)采用交叉熵,激活函數(shù)采用ReLU,優(yōu)化器采用帶有動(dòng)量的mini-batchSGD算法,需要的朋友可以參考一下
    2022-03-03
  • Python使用win32com實(shí)現(xiàn)的模擬瀏覽器功能示例

    Python使用win32com實(shí)現(xiàn)的模擬瀏覽器功能示例

    這篇文章主要介紹了Python使用win32com實(shí)現(xiàn)的模擬瀏覽器功能,結(jié)合實(shí)例形式分析了Python基于win32com模塊實(shí)現(xiàn)網(wǎng)頁的打開、登陸、加載等功能相關(guān)技巧,需要的朋友可以參考下
    2017-07-07
  • Python實(shí)現(xiàn)全角半角字符互轉(zhuǎn)的方法

    Python實(shí)現(xiàn)全角半角字符互轉(zhuǎn)的方法

    大家都知道在自然語言處理過程中,全角、半角的的不一致會(huì)導(dǎo)致信息抽取不一致,因此需要統(tǒng)一。這篇文章通過示例代碼給大家詳細(xì)的介紹了Python實(shí)現(xiàn)全角半角字符互轉(zhuǎn)的方法,有需要的朋友們可以參考借鑒,下面跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。
    2016-11-11
  • 使用Python和GDAL給圖片加坐標(biāo)系的實(shí)現(xiàn)思路(坐標(biāo)投影轉(zhuǎn)換)

    使用Python和GDAL給圖片加坐標(biāo)系的實(shí)現(xiàn)思路(坐標(biāo)投影轉(zhuǎn)換)

    這篇文章主要介紹了使用Python和GDAL給圖片加坐標(biāo)系的實(shí)現(xiàn)思路(坐標(biāo)投影轉(zhuǎn)換),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • 在 Python 中接管鍵盤中斷信號(hào)的實(shí)現(xiàn)方法

    在 Python 中接管鍵盤中斷信號(hào)的實(shí)現(xiàn)方法

    要使用信號(hào),我們需用導(dǎo)入 Python 的signal庫(kù)。然后自定義一個(gè)信號(hào)回調(diào)函數(shù),當(dāng) Python 收到某個(gè)信號(hào)時(shí),調(diào)用這個(gè)函數(shù)。 ,下面通過實(shí)例代碼給大家介紹在 Python 中接管鍵盤中斷信號(hào),需要的朋友可以參考下
    2020-02-02
  • 分布式全文檢索引擎ElasticSearch原理及使用實(shí)例

    分布式全文檢索引擎ElasticSearch原理及使用實(shí)例

    這篇文章主要介紹了分布式全文檢索引擎ElasticSearch原理及使用實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • Python DataFrame一列拆成多列以及一行拆成多行

    Python DataFrame一列拆成多列以及一行拆成多行

    這篇文章主要介紹了Python DataFrame一列拆成多列以及一行拆成多行,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • Python操作Excel神器openpyxl使用教程(超詳細(xì)!)

    Python操作Excel神器openpyxl使用教程(超詳細(xì)!)

    openpyxl庫(kù)是一個(gè)很好處理xlsx的python庫(kù),下面這篇文章主要給大家介紹了關(guān)于Python辦公自動(dòng)化openpyxl使用的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-01-01
  • python讀取并寫入mat文件的方法

    python讀取并寫入mat文件的方法

    這篇文章主要介紹了python讀取并寫入mat文件的方法,文中給大家提到了python讀取matlab寫的mat文件問題以及解決辦法 ,需要的朋友可以參考下
    2019-07-07
  • 使用OpenCV circle函數(shù)圖像上畫圓的示例代碼

    使用OpenCV circle函數(shù)圖像上畫圓的示例代碼

    這篇文章主要介紹了使用OpenCV circle函數(shù)圖像上畫圓的示例代碼,本文內(nèi)容簡(jiǎn)短,給大家突出重點(diǎn)內(nèi)容,需要的朋友可以參考下
    2019-12-12

最新評(píng)論

疏勒县| 友谊县| 临澧县| 伊吾县| 太仆寺旗| 鸡西市| 公安县| 清远市| 久治县| 杨浦区| 崇信县| 宁强县| 云南省| 敖汉旗| 旬阳县| 安顺市| 宜黄县| 诏安县| 射阳县| 喀喇沁旗| 昌江| 台山市| 齐河县| 穆棱市| 建水县| 安仁县| 玉屏| 马山县| 迁安市| 远安县| 英吉沙县| 湘乡市| 洛隆县| 偃师市| 景谷| 宿州市| 伊吾县| 盱眙县| 建始县| 视频| 仙居县|