flask上傳作品之dbm操作的實(shí)現(xiàn)
一,前言
今天做的東西,還算可以,修改了若干個(gè)bug,自己又寫成功的寫了幾個(gè)bug。增加了一個(gè)功能——手動(dòng)控制歷史比賽【主要通過dbm數(shù)據(jù)庫(kù)的操作來控制】
需求:
活動(dòng)需要手動(dòng)定為歷史活動(dòng),不要按照年份自動(dòng)變?yōu)闅v史活動(dòng)。
二,dbm數(shù)據(jù)庫(kù)操作-match_set
2.1,后端響應(yīng)請(qǐng)求
增加了應(yīng)該路由-match_set。
上文代碼塊解釋:
這部分是,前端用戶請(qǐng)求match_set,后端返回給前端的內(nèi)容。
原理就是遍歷我們需要的dbm數(shù)據(jù)庫(kù)里面的內(nèi)容,放到合適的位置。傳遞給前端。
如下:
022|包頭市第二界文化旅游創(chuàng)意作品大賽|2,2023|朱博的個(gè)人作品大賽|,2024|朱博的大賽|,2025|朱博我的的大賽|,2026|朱博我的的大賽|,2027|朱博我的你的的
大賽|,2028|朱博我的你的的大賽|,2029|朱博我的你的的大賽|,2030|朱博我的你的的大賽|', ‘pw_nums’: ‘3’, ‘btn_3’: ‘大賽成績(jī)|/result’, ‘btn_1’: ‘上傳
作品|/new_post’, ‘match_name’: ‘朱博我的你的的大賽’, ‘upload’: ‘false’, ‘btn_4’: ‘往期回顧|/past’, ‘time’: ‘1’, ‘match’: ‘2030’, ‘time_3_posts’
: ‘’, ‘btn_5’: ‘評(píng)委入口|/admin’, ‘time_2_posts’: ‘1’, ‘switch’: ‘’, ‘picture’: ‘’, ‘image’: ‘u=2566611882,1193878858&fm=253&fmt=auto&app=138&f
=JPEG.webp’
測(cè)試用的,有點(diǎn)亂哈哈。
2.2,前端返回表單
<label for="">目標(biāo)賽事名稱:</label> <input type="text" name="match_name" {% if time %}value="{{match_name}}"{% endif %}> <label for="">目標(biāo)賽事標(biāo)識(shí):</label> <input type="text" name="match" {% if time %}value="{{match}}"{% endif %}> <label for="">目標(biāo)賽事設(shè)置(1:不設(shè)置為歷史歷史賽事 2:設(shè)置為歷史歷史賽事):</label> <input type="text" name="switch"> <button class="button button--secondary" type="submit">提交</button>
前端如上,非常簡(jiǎn)單的一個(gè)收集表單。
前端發(fā)起提交按鈕,提交給后端,后端再處理。
2.3,后端再次響應(yīng),保存操作
if request.method == "POST":
上文代碼解釋:判斷,前端是以post的方式傳到后端的。
if key == "match": #print("當(dāng)前路徑為1") if "past_matchs" in db_config: if value not in db_config["past_matchs"].decode(): flag=0 else: #print("當(dāng)前路徑為4") past_matchss = db_config["past_matchs"].decode().split(',') #print(db_config["past_matchs"].decode()) #print(past_matchss) for i in past_matchss: jj=i break
上文代碼塊解釋:【這一塊代碼很多。展示部分代碼,下文貼全部代碼?!?br />這里的邏輯即為復(fù)雜,因?yàn)楸闅v的時(shí)候,我們之間遍歷他的key和value。
我們找到目標(biāo)key == “match”:。
下文判斷。如果我們的dbm數(shù)據(jù)庫(kù)里含有past_matchs的話執(zhí)行{
如果我們的目標(biāo)value值不再dbm數(shù)據(jù)庫(kù)里,我們吧flag賦值為0.下面判斷,如果flag==0說明,{該賽事不存在}
如果value值在dbm數(shù)據(jù)庫(kù)里{
past_matchss = db_config["past_matchs"].decode().split(',')
獲取到目標(biāo)值。
如上文代碼邏輯?!敬篌w來講:我重新寫了一個(gè)類別,因?yàn)椴荒軇h除的原因,我選擇重新賦值遍歷,累加】
最后:
if(flag==0): flash("該賽事不存在") if(flag==1): db.session.commit() flash("提交成功", "success") return redirect(url_for('match_set'))
上文代碼解釋:
因?yàn)槲覀冎敖oflag賦值為1。如果沒有目標(biāo)value在我們的數(shù)據(jù)庫(kù)里的話,falg會(huì)賦值為0。
不執(zhí)行更改數(shù)據(jù)庫(kù)的操作。反之更改。
最后重定向到目標(biāo)頁面,此部分結(jié)束!
三,附代碼
@min_role_required(ROLES["mod"]) @app.route('/match_set/', methods=['GET', 'POST']) def match_set(): flag=1 if request.method == "POST": switch1='' for key, value in request.form.items(): if key == "switch": switch1=value for key, value in request.form.items(): # print("標(biāo)記key",key) # print("標(biāo)記value",value) if key == "match": #print("當(dāng)前路徑為1") if "past_matchs" in db_config: if value not in db_config["past_matchs"].decode(): flag=0 else: #print("當(dāng)前路徑為4") past_matchss = db_config["past_matchs"].decode().split(',') #print(db_config["past_matchs"].decode()) #print(past_matchss) for i in past_matchss: jj=i break iip=0 for i in past_matchss: if(iip>0): #print(i) if(value not in i): jj=jj+","+i if(value in i): #print(switch1) if(i.split("|")[2]==switch1): jj=jj+","+i #print("正確") else: #print(value+"|"+request.form.get('match_name')+"|"+switch1) jj=jj+","+(value+"|"+request.form.get('match_name')+"|"+switch1) #print("錯(cuò)誤") #print(i) iip=iip+1 db_config["past_matchs"]=jj else: db_config["past_matchs"] = f"{value}|{request.form.get('match_name')}|{request.form.get('switch')}" #print("當(dāng)前路徑為3") if(flag==0): flash("該賽事不存在") if(flag==1): db.session.commit() flash("提交成功", "success") return redirect(url_for('match_set')) attrs_dict = {} for k in db_config.keys(): attrs_dict[k.decode()] = db_config[k].decode() #print(attrs_dict) past_matchs = db_config["past_matchs"].decode().split(',') past_qian=db_config["match"].decode() pasts = [] for match_tuple in past_matchs: if not match_tuple: continue match_mark = match_tuple.split("|")[0] match_name = match_tuple.split("|")[1] match_zhuangtai = match_tuple.split("|")[2] data = {} data["title"] = match_name data["count"] = Post.query.filter_by(match=match_mark).count() data["url"] = url_for('index', match=match_mark) data["zhuangtai"] = match_zhuangtai data["muqian"] = past_qian data["biaoshi"]=match_mark pasts.append(data) #return render_template('past.html', pasts=pasts) return render_template('admin/match_set.html', pasts=pasts)
到此這篇關(guān)于flask上傳作品之dbm操作的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)flask dbm操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python深度學(xué)習(xí)實(shí)戰(zhàn)PyQt5信號(hào)與槽的連接
本文講解信號(hào)與槽的連接機(jī)制,詳細(xì)示范各種類型的信號(hào)/槽連接的實(shí)現(xiàn)方法,這是圖形用戶界面的核心內(nèi)容。還將介紹面向?qū)ο蟮某绦蛟O(shè)計(jì),這是圖形用戶界面的基本思想2021-10-10Python作用域(局部?全局)及global關(guān)鍵字使用詳解
這篇文章主要為大家介紹了Python作用域(局部?全局)及global關(guān)鍵字使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10解決Python復(fù)雜zip文件的解壓?jiǎn)栴}
這篇文章主要介紹了Python復(fù)雜zip文件的解壓,通過配合 shutil 與 os 標(biāo)準(zhǔn)庫(kù)中的相關(guān)功能,實(shí)現(xiàn)將指定任意 zip 壓縮包,完好地解壓到指定的目錄中,需要的朋友可以參考下2021-12-12python 兩個(gè)一樣的字符串用==結(jié)果為false問題的解決
這篇文章主要介紹了python 兩個(gè)一樣的字符串用==結(jié)果為false問題的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03python3實(shí)現(xiàn)語音轉(zhuǎn)文字(語音識(shí)別)和文字轉(zhuǎn)語音(語音合成)
這篇文章主要介紹了python3實(shí)現(xiàn)語音轉(zhuǎn)文字(語音識(shí)別)和文字轉(zhuǎn)語音(語音合成),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10jupyter notebook 添加kernel permission denied的操作
這篇文章主要介紹了jupyter notebook 添加kernel permission denied的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04python unichr函數(shù)知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是一篇關(guān)于python unichr函數(shù)的知識(shí)點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2020-12-12Python實(shí)現(xiàn)的KMeans聚類算法實(shí)例分析
這篇文章主要介紹了Python實(shí)現(xiàn)的KMeans聚類算法,結(jié)合實(shí)例形式較為詳細(xì)的分析了KMeans聚類算法概念、原理、定義及使用相關(guān)操作技巧,需要的朋友可以參考下2018-12-12