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