django的ORM操作 刪除和編輯實現(xiàn)詳解
向server端傳送數(shù)據(jù)
有2中方法,1 是 通過url 地址, 2 是通過路徑
向server端傳參數(shù)方式
1,通過數(shù)據(jù) http://127.0.0.1:8000/blog/?id=2
2, 通過路徑 http://17.0.0.1:8000/blog/20
# url(r'blog/(\d{4})')
刪除功能:
在url文件中,創(chuàng)建一個delbook路徑, 通過url的地址拿到id實現(xiàn)刪除
urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^$',views.index),#指定一個根目錄,也指向index頁面 url(r'^index/$',views.index), url(r'^addbook/$',views.addbook), url(r'^delbook/$',views.delbook), -------------------------------------------del刪除功能,對應(yīng)視圖函數(shù) #(\d+)分組后,作為參數(shù)傳給editorbook函數(shù),editorbook(request,1或2 等等) url(r'^editorbook/(\d+)',views.editorbook), ]
在index.html 頁面中,點擊刪除按鈕,在href 加上?id={{ book.id}}要刪除的書籍,
在get請求時,url加上刪除時點擊到的id,獲取id,就可以刪除#}
<a href="/delbook/?id={{ book.id }}" rel="external nofollow" rel="external nofollow" ><button class="btn btn-primary">刪除</button></a>
在刪除一條記錄后,頁面的順序是錯亂,在前端顯示的是數(shù)據(jù)庫的id,用forloop.counter 默認(rèn)從1開始循環(huán)顯示,與數(shù)據(jù)庫的id無關(guān),
<td>{{ forloop.counter }}</td>
{% load staticfiles %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="{% static '/bootstrap-3.3.7/dist/css/bootstrap.css/' %}" rel="external nofollow" > <style> .container{ margin-top: 50px; } </style> </head> <body> <div class="container"> <div class="row"> <div class="col-md-8 col-md-offset-2"> <table class="table table-striped"> <tr> <th>ID</th> <th>書名</th> <th>價格</th> <th>出版日期</th> <th>作者</th> <th>出版社</th> <th>分類</th> <th>操作</th> </tr> {% for book in book_list %} <tr> {# 在前端顯示的是數(shù)據(jù)庫的id,用forloop.counter 默認(rèn)從1開始循環(huán)顯示,與數(shù)據(jù)庫的id無關(guān), <td>{{ book.id }}</td>#} <td>{{ forloop.counter }}</td>--------------------按照順序顯示, <td>{{ book.name }}</td> <td>{{ book.price }}</td> <td>{{ book.Date }}</td> <td>{{ book.auth }}</td> <td>{{ book.publish }}</td> <td>{{ book.classification }}</td> <td> {# 當(dāng)前的ip和端口都可以省略,會自動添加,a標(biāo)簽會訪問addbook路徑#} <a href="/addbook/" rel="external nofollow" ><button class="btn btn-primary">添加</button></a> {# 在get請求時,url加上刪除時點擊到的id,獲取id,就可以刪除#} <a href="/delbook/?id={{ book.id }}" rel="external nofollow" rel="external nofollow" ><button class="btn btn-primary">刪除</button></a> {# 取到路徑,#} <a href="/editorbook/{{ book.id }}" rel="external nofollow" ><button class="btn btn-primary">編輯</button></a> </td>d </tr> {% endfor %} </table> </div> </div> </div> </body> <script> </script> </html>
在views文件中,編輯delbook函數(shù),
django里的刪除和編輯,前提都是 要先找到,利用filter()方法,條件是id或者是name,等都可以,
步驟1,用get的方法,從url路徑中拿到id,
步驟2,對數(shù)據(jù)庫的id和要url里獲取到的id,對應(yīng),就執(zhí)行delete()方法,就刪除指定的記錄,數(shù)據(jù)庫也會減少一條記錄,
#刪除和修改,都是要先找到記錄(對象) def delbook(request): #先過濾,加上過濾的條件,然后用delete() #向server端傳參數(shù)方式 #1,通過數(shù)據(jù) http://127.0.0.1:8000/blog/?id=2 #2, 通過路徑 http://17.0.0.1:8000/blog/20 # url(r'blog/(\d{4})') #在前端頁面加上id值,{{book.id}} #通過url獲取iD,是get的方法,"id"是url里的key, id = request.GET.get("id") #前面的id是表里的字段,把get從地址欄里獲取到的id賦值給表里的id,就可以刪除 # Book.objects.filter(id = id).delete() return redirect('/index/')
=======
ORM的編輯功能
在url文件中創(chuàng)建editorbook路徑,和映射到視圖函數(shù),
通過訪問的路徑,拿到id,
urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^$',views.index),#指定一個根目錄,也指向index頁面 url(r'^index/$',views.index), url(r'^addbook/$',views.addbook), url(r'^delbook/$',views.delbook), -------------------------------------------del刪除功能,對應(yīng)視圖函數(shù) #(\d+)分組后,作為參數(shù)傳給editorbook函數(shù),editorbook(request,1或2 等等) url(r'^editorbook/(\d+)',views.editorbook),-----------------------------editorbook 編輯功能,對應(yīng)一個視圖函數(shù) ]
編輯一個editorbook頁面,
編輯時要獲取要編輯的是哪個對象,
比如:blog/20 ,20就是要獲取到的id
通過路徑 http://17.0.0.1:8000/blog/20
# url(r'blog/(\d{4})')
{% load staticfiles %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="{% static '/bootstrap-3.3.7/dist/css/bootstrap.css/' %} " rel="external nofollow" > <style> .container{ margin-top: 50px; } </style> </head> <body> <div class="container"> <div class="row"> <div class="col-md-6 col-md-offset-2"> {# {{ csrf-token }}#} {# 以post的方法提交數(shù)據(jù),走到editbook 視圖函數(shù)#} <form class=editorbook" action="/editorbook/{{ book_obj.id }}/" method="post"> -----------加上從數(shù)據(jù)庫里拿到的id號, {# 當(dāng)點擊添加按鈕時,是執(zhí)行視圖函數(shù),在數(shù)據(jù)庫中添加一條記錄,然后再把這個記錄添加到index頁面#} <div class="form-group"> <label for="bookname">書名:</label> <input type="text" class="form-control" id="bookname" name="bookname" value="{{ book_obj.name }}"> </div> <div class="form-group"> <label for="price">價格:</label> <input type="text" class="form-control" id="price" name="price" value="{{ book_obj.price }}"> </div> <div class="form-group"> <label for="Date">日期:</label> <input type="text" class="form-control" id="Date" name="Date" value="{{ book_obj.Date }}"> </div> <div class="form-group"> <label for="auth">作者:</label> <input type="text" class="form-control" id="auth" name="auth" value="{{ book_obj.auth }}"> </div> <div class="form-group"> <label for="publish">出版社:</label> <input type="text" class="form-control" id="publish" name="publish" value="{{ book_obj.publish }}"> </div> <div class="form-group"> <label for="publish">分類:</label> <input type="text" class="form-control" id="publish" name="classification" value="{{ book_obj.classification }}"> </div> <input class="btn btn-info" type="submit" value='提交'> </form> </div> </div> </div> </body> </html>
在views文件中,撰寫editorbook函數(shù),
表單的提交是post 方法,用post的方法獲取到每個輸入框的值, 然后保存到數(shù)據(jù)庫
def editorbook(request,id): # 2, 通過路徑 http://17.0.0.1:8000/blog/20 # url(r'blog/(\d{4})') #通過id獲取要修改的對象,在前端中把對象的每個字段屬性給value,就可以在input框顯示要編輯的值 # book_obj = Book.objects.filter(id = id)[0] book_obj = Book.objects.filter(id = id).first() if request.method == "POST": bookname = request.POST.get('bookname') price = request.POST.get('price') Date = request.POST.get('Date') auth = request.POST.get('auth') publish = request.POST.get('publish') classification = request.POST.get('classification') #方法2 update修改并保存數(shù)據(jù) ,name 是數(shù)據(jù)庫的字段,bookname是前端form表單里 的name屬性值,把輸入框里獲取到的值給數(shù)據(jù)庫的字段進(jìn)行保存, Book.objects.filter(id = id).update(name = bookname,price = price, Date = Date, auth = auth , publish = publish, classification = classification) return redirect('/index/') return render(request,'editorbook.html',{'book_obj':book_obj})
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
pymongo給mongodb創(chuàng)建索引的簡單實現(xiàn)方法
這篇文章主要介紹了pymongo給mongodb創(chuàng)建索引的簡單實現(xiàn)方法,涉及Python使用pymongo模塊操作mongodb的技巧,需要的朋友可以參考下2015-05-05Python帶動態(tài)參數(shù)功能的sqlite工具類
這篇文章主要介紹了Python帶動態(tài)參數(shù)功能的sqlite工具類,涉及Python針對sqlite數(shù)據(jù)庫的連接、查詢、sql語句執(zhí)行等相關(guān)操作封裝與使用技巧,需要的朋友可以參考下2018-05-05ActiveMQ:使用Python訪問ActiveMQ的方法
今天小編就為大家分享一篇ActiveMQ:使用Python訪問ActiveMQ的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01Python實現(xiàn)的微信好友數(shù)據(jù)分析功能示例
這篇文章主要介紹了Python實現(xiàn)的微信好友數(shù)據(jù)分析功能,結(jié)合實例形式分析了Python使用itchat、pandas、pyecharts等模塊針對微信好友數(shù)據(jù)進(jìn)行統(tǒng)計與計算相關(guān)操作技巧,需要的朋友可以參考下2018-06-06Pytorch中expand()的使用(擴(kuò)展某個維度)
這篇文章主要介紹了Pytorch中expand()的使用(擴(kuò)展某個維度),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07influx+grafana自定義python采集數(shù)據(jù)和一些坑的總結(jié)
一些數(shù)據(jù)的類型不正確會導(dǎo)致no datapoint的錯誤,真是令人抓狂,本文就是總結(jié)一下采集數(shù)據(jù)種的一些坑,希望大家可以從中獲益2018-09-09深入理解Python虛擬機(jī)中字節(jié)(bytes)的實現(xiàn)原理及源碼剖析
在本篇文章當(dāng)中主要給大家介紹在?cpython?內(nèi)部,bytes?的實現(xiàn)原理、內(nèi)存布局以及與?bytes?相關(guān)的一個比較重要的優(yōu)化點——?bytes?的拼接,需要的可以參考一下2023-03-03python如何將文件a.txt的內(nèi)容復(fù)制到b.txt中
這篇文章主要介紹了python如何將文件a.txt的內(nèi)容復(fù)制到b.txt中,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12