Python + Flask 專案開發實踐系列《五》
阿新 • • 發佈:2020-08-27
歡迎關注【無量測試之道】公眾號,回覆【領取資源】,
Python程式設計學習資源乾貨、
Python+Appium框架APP的UI自動化、
Python+Selenium框架Web的UI自動化、
Python+Unittest框架API自動化、
資源和程式碼 免費送啦~
文章下方有公眾號二維碼,可直接微信掃一掃關注即可。
今天開始我們講講Flask Web實踐專案開發中的查詢功能是如何實現的。
Step1:html 部分
1 <div class="row" align="right"> 2 主要內容:<input type='text' id='contents' name='contents'> 3 <button class="btn btn-warning" id="select">查詢</button> 4 <button class="btn btn-primary" id="adds">新增</button> 5 <button class="btn btn-danger" id="delete">刪除</button> 6 </div>
Step2:javascript部分
1 2 $(function () { 3 $('#select').click(function(){ 4 callback1(1,"none") 5 }); 6 var callback1= function (page,contents){ 7 var contents=$('#contents').val(); 8 if(contents.length==0){ 9 alert("輸出內容不能為空!"); 10 return ; 11 } 12 $.ajax({13 url: '/select/'+page+"/"+contents, 14 type: 'GET', 15 dataType: 'json', 16 async:false, 17 timeout: 1000, 18 cache: false, 19 beforeSend: LoadFunction, //載入執行方法 20 error: ErrFunction, //錯誤執行方法 21 success: function (data) { //成功執行的方法 22 var jsons=data ? data : []; 23 // 分頁處理 24 $("#pageid").pager({ 25 pagenumber: jsons.pageNum, 26 pagecount: jsons.pages, 27 totalcount: jsons.amount, 28 buttonClickCallback: callback1 29 }); 30 lists="" 31 $.each(jsons.content, function (index, item) {//迴圈獲取資料 32 lists +="<tr>"+ 33 "<td style='word-wrap:break-word;word-break:break-all; '><input type='checkbox' id='itemid' name='testid' value='"+item.id+"'>"+item.id+"</td>"+ 34 "<td style='word-wrap:break-word;word-break:break-all; '>"+item.pms_name+"</td>"+ 35 "<td style='word-wrap:break-word;word-break:break-all; '>"+item.content+"</td>"+ 36 "<td style='word-wrap:break-word;word-break:break-all; '>"+item.status+"</td>"+ 37 "<td style='word-wrap:break-word;word-break:break-all; '>"+item.mark+"</td>"+ 38 "<td style='word-wrap:break-word;word-break:break-all; '>"+item.create_time+"</td>"+ 39 "<td style='word-wrap:break-word;word-break:break-all; '>" + 40 "<button class='btn btn-info' id='update' align='center' onclick='update($(this))'>修改</button>  "+ 41 "<button class='btn btn-warning' id='updateother' align='center' onclick='detail($(this))'>檢視詳情</button>" + 42 "</td>" 43 "</tr>" 44 }); 45 $("#alldatas").html(lists); 46 } 47 }) 48 }; 49 function LoadFunction() { 50 $("#alldatas").html('載入的資料未找到,請重新新增!...'); 51 } 52 function ErrFunction() { 53 alert("資料載入失敗!!!!"); 54 } 55 });
Step3:Python+Flask 部分
1 @app.route('/select/<page>/<contents>',methods=['get']) 2 def select(page,contents): 3 contents="'%"+str(contents)+"%'" 4 sql = "select count(*) from flask_info where content like "+contents 5 PageCount=10 6 All_Record = get_count(sql) 7 if (int(All_Record) % int(PageCount) == 0): 8 All_page = All_Record / PageCount 9 else: 10 All_page = All_Record / PageCount + 1 11 tiao=(int(page)-1)*int(PageCount) 12 print(contents) 13 sql1="select id,pms_name,content,status,mark,create_time from flask_info where content like "+contents+" order by create_time desc limit %s,%s"%(tiao,PageCount) 14 print(sql) 15 content=get_data(sql1) 16 pagedict={} 17 pagedict['content']=content 18 pagedict['pageNum']=page 19 pagedict['pages']=All_page 20 pagedict['amount']=All_Record 21 return jsonify(pagedict)
Step4: db部分
1 def get_data(sql1):#獲取sql返回記錄數 2 db = sqlite3.connect('test_flask.db') 3 cur = db.cursor() 4 print(sql1) 5 cur.execute(sql1) 6 results=cur.fetchall() 7 cloumn=get_table_colum() 8 res = {} 9 reslist = [] 10 print(results) 11 for r in range(len(list(results))): 12 for m in range(len(list(cloumn))): 13 res[str(list(cloumn)[m])] = str(list(results)[r][m]) 14 reslist.append(res) 15 res = {} 16 print(reslist) 17 cur.close() 18 db.close() 19 return reslist
頁面查詢效果如下圖所示:
總結:本篇文章主要是通過一個文字框輸入,然後點選查詢按鈕提交一個查詢請求,請求中會帶上被輸入的內容進行查詢匹配,等待查詢出來的資料集進行頁面資料的渲染即可。
備註:我的個人公眾號已正式開通,致力於測試技術的分享,包含:大資料測試、功能測試,測試開發,API介面自動化、測試運維、UI自動化測試等,微信搜尋公眾號:“無量測試之道”,或掃描下方二維碼:
新增關注,讓我們一起共同成長!