向server端傳送數據,
有2中方法,1 是 經過url 地址, 2 是經過路徑
#向server端傳參數方式 #1,經過數據 http://127.0.0.1:8000/blog/?id=2 #2, 經過路徑 http://17.0.0.1:8000/blog/20 # url(r'blog/(\d{4})')
刪除功能:css
在url文件中,建立一個delbook路徑, 經過url的地址拿到id實現刪除html
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刪除功能,對應視圖函數 #(\d+)分組後,做爲參數傳給editorbook函數,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 }}"><button class="btn btn-primary">刪除</button></a>
在刪除一條記錄後,頁面的順序是錯亂,在前端顯示的是數據庫的id,用forloop.counter 默認從1開始循環顯示,與數據庫的id無關,數據庫
<td>{{ forloop.counter }}</td>django
{% 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/' %}"> <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> {# 在前端顯示的是數據庫的id,用forloop.counter 默認從1開始循環顯示,與數據庫的id無關, <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> {# 當前的ip和端口均可以省略,會自動添加,a標籤會訪問addbook路徑#} <a href="/addbook/"><button class="btn btn-primary">添加</button></a> {# 在get請求時,url加上刪除時點擊到的id,獲取id,就能夠刪除#} <a href="/delbook/?id={{ book.id }}"><button class="btn btn-primary">刪除</button></a> {# 取到路徑,#} <a href="/editorbook/{{ book.id }}"><button class="btn btn-primary">編輯</button></a> </td>d </tr> {% endfor %} </table> </div> </div> </div> </body> <script> </script> </html>
在views文件中,編輯delbook函數,bootstrap
django裏的刪除和編輯,前提都是 要先找到,利用filter()方法,條件是id或者是name,等均可以,函數
步驟1,用get的方法,從url路徑中拿到id,oop
步驟2,對數據庫的id和要url裏獲取到的id,對應,就執行delete()方法,就刪除指定的記錄,數據庫也會減小一條記錄,post
#刪除和修改,都是要先找到記錄(對象) def delbook(request): #先過濾,加上過濾的條件,而後用delete() #向server端傳參數方式 #1,經過數據 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/')
=======url
ORM的編輯功能
在url文件中建立editorbook路徑,和映射到視圖函數,
經過訪問的路徑,拿到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刪除功能,對應視圖函數
#(\d+)分組後,做爲參數傳給editorbook函數,editorbook(request,1或2 等等)
url(r'^editorbook/(\d+)',views.editorbook),-----------------------------editorbook 編輯功能,對應一個視圖函數
]
編輯一個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/' %} "> <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的方法提交數據,走到editbook 視圖函數#} <form class=editorbook" action="/editorbook/{{ book_obj.id }}/" method="post"> -----------加上從數據庫裏拿到的id號, {# 當點擊添加按鈕時,是執行視圖函數,在數據庫中添加一條記錄,而後再把這個記錄添加到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函數,
表單的提交是post 方法,用post的方法獲取到每一個輸入框的值, 而後保存到數據庫
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修改並保存數據 ,name 是數據庫的字段,bookname是前端form表單裏 的name屬性值,把輸入框裏獲取到的值給數據庫的字段進行保存, 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})
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修改並保存數據
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})