电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

瀏覽:30日期:2022-06-20 08:37:32

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

0x01路由

from flask import Flaskapp = Flask(__name__) # flask對(duì)象實(shí)例化 @app.route(’/index’) #定義首頁(yè)@app.route(’/’) #設(shè)置默認(rèn)indexdef 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 #開(kāi)啟debug模式 app.run()0x02 模版和靜態(tài)文件2.1 文件結(jié)構(gòu)

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

2.2代碼

#app.py#app.pyfrom 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='http://www.hdgsjgj.cn/static/imgs/1.png'></body></html>2.3 運(yùn)行效果

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

0x03 json

from flask import Flask,jsonifyapp = Flask(__name__)@app.route(’/’)def index(): user = {’name’:’李三’,’password’:’123’} return jsonify(user)if __name__ == ’__main__’: app.run(debug=True)3.1運(yùn)行效果

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

0x04 重定向4.1 訪問(wèn)跳轉(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)路徑,訪問(wèn)/index目錄即跳到百度首頁(yè)@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)傳參

# 訪問(wèn)home,將name帶入index并顯示在頁(yè)面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 }} #字母大寫(xiě)(更多可查閱jinjia2過(guò)濾器)</body></html>5.2 運(yùn)行效果

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

0x06 藍(lán)圖

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

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

├── admin│ └── admin.py└── app.py6.2 代碼

#admin.pyfrom flask import Blueprint 導(dǎo)入藍(lán)圖模塊admin = Blueprint(’admin’,__name__,url_prefix=’/admin’) #對(duì)象實(shí)例化,url_prefix添加路由前綴,表示若想訪問(wèn)本頁(yè)相關(guān)路由,只能通過(guò)形如 xxx/admin/login 訪問(wèn),不能 xxx/login訪問(wèn)@admin.route(’/register’)def register(): return ’歡迎注冊(cè)’@admin.route(’/login’)def login(): return ’歡迎登錄’

#app.pyfrom flask import Flaskfrom 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)

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

7.2代碼

#web.pyfrom flask import Flask,render_template,request,redirect,flash,url_for,sessionfrom os import urandomapp = 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 role='alert'> <button type='button' 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 role='alert'> <button type='button' 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)到登錄頁(yè)面

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

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

賬戶密碼:admin/888888

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

7.3.2登錄失敗效果

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

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

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: NMRV减速机|铝合金减速机|蜗轮蜗杆减速机|NMRV减速机厂家-东莞市台机减速机有限公司 | hc22_hc22价格_hc22哈氏合金—东锜特殊钢 | 首页-恒温恒湿试验箱_恒温恒湿箱_高低温试验箱_高低温交变湿热试验箱_苏州正合 | 金联宇电缆|广东金联宇电缆厂家_广东金联宇电缆实业有限公司 | 聚合氯化铝厂家-聚合氯化铝铁价格-河南洁康环保科技 | 行星齿轮减速机,减速机厂家,山东减速机-淄博兴江机械制造 | 办公室家具公司_办公家具品牌厂家_森拉堡办公家具【官网】 | 玻璃钢板-玻璃钢防腐瓦-玻璃钢材料-广东壹诺 | 印刷人才网 印刷、包装、造纸,中国80%的印刷企业人才招聘选印刷人才网! | 正压密封性测试仪-静态发色仪-导丝头柔软性测试仪-济南恒品机电技术有限公司 | 安全光栅|射频导纳物位开关|音叉料位计|雷达液位计|两级跑偏开关|双向拉绳开关-山东卓信机械有限公司 | 密集架-密集柜厂家-智能档案密集架-自动选层柜订做-河北风顺金属制品有限公司 | 磁棒电感生产厂家-电感器厂家-电感定制-贴片功率电感供应商-棒形电感生产厂家-苏州谷景电子有限公司 | 乐之康护 - 专业护工服务平台,提供医院陪护-居家照护-居家康复 | 成都热收缩包装机_袖口式膜包机_高速塑封机价格_全自动封切机器_大型套膜机厂家 | 红立方品牌应急包/急救包加盟,小成本好项目代理_应急/消防/户外用品加盟_应急好项目加盟_新奇特项目招商 - 中红方宁(北京) 供应链有限公司 | 流量卡中心-流量卡套餐查询系统_移动电信联通流量卡套餐大全 | 驾驶人在线_专业学车门户网站 | 石家庄装修设计_室内家装设计_别墅装饰装修公司-石家庄金舍装饰官网 | 合肥触摸一体机_触摸查询机厂家_合肥拼接屏-安徽迅博智能科技 | 锂离子电池厂家-山东中信迪生电源 | 浙江华锤电器有限公司_地磅称重设备_防作弊地磅_浙江地磅售后维修_无人值守扫码过磅系统_浙江源头地磅厂家_浙江工厂直营地磅 | 西门子伺服电机维修,西门子电源模块维修,西门子驱动模块维修-上海渠利 | 打造全球沸石生态圈 - 国投盛世 锂电混合机-新能源混合机-正极材料混料机-高镍,三元材料混料机-负极,包覆混合机-贝尔专业混合混料搅拌机械系统设备厂家 | 威实软件_软件定制开发_OA_OA办公系统_OA系统_办公自动化软件 | 高压油管,液压接头,液压附件-烟台市正诚液压附件 | 成都租车_成都租车公司_成都租车网_众行宝 | 股指期货-期货开户-交易手续费佣金加1分-保证金低-期货公司排名靠前-万利信息开户 | 不锈钢复合板厂家_钛钢复合板批发_铜铝复合板供应-威海泓方金属复合材料股份有限公司 | 北京开源多邦科技发展有限公司官网 | 旋振筛_不锈钢旋振筛_气旋筛_旋振筛厂家—新乡市大汉振动机械有限公司 | 卫生纸复卷机|抽纸机|卫生纸加工设备|做卫生纸机器|小型卫生纸加工需要什么设备|卫生纸机器设备多少钱一台|许昌恒源纸品机械有限公司 | 周口市风机厂,周鼓风机,河南省周口市风机厂 | 工控机,嵌入式主板,工业主板,arm主板,图像采集卡,poe网卡,朗锐智科 | BOE画框屏-触摸一体机-触控查询一体机-触摸屏一体机价格-厂家直销-触发电子 | 纯化水设备-EDI-制药-实验室-二级反渗透-高纯水|超纯水设备 | 政府园区专业委托招商平台_助力企业选址项目快速落地_东方龙商务集团 | 通辽信息港 - 免费发布房产、招聘、求职、二手、商铺等信息 www.tlxxg.net | 广州/东莞小字符喷码机-热转印打码机-喷码机厂家-广州瑞润科技 | IHDW_TOSOKU_NEMICON_EHDW系列电子手轮,HC1系列电子手轮-上海莆林电子设备有限公司 | 轻型地埋电缆故障测试仪,频响法绕组变形测试仪,静荷式卧式拉力试验机-扬州苏电 |