這篇文章給大家介紹使用django框架怎么實(shí)現(xiàn)單表增刪改操作,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

代碼如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css" rel="external nofollow" >
<title>書列表</title>
</head>
<body>
<div class="container">
<a href="/add_book/" rel="external nofollow" class="btn btn-success">添加新書</a>
<div class="panel panel-primary">
<div class="panel-heading">書籍管理</div>
<div class="panel-body">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>#</th>
<th>書名</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{% for book in book_list %}
<tr data-id="{{ book.id }}">
<td>{{ forloop.counter }}</td>
<td>{{ book.title }}</td>
<td><a href="/delete_book/?id={{ book.id }}" rel="external nofollow" class="btn btn-danger">刪除</a>
<a href="/edit_book/?id={{ book.id }}" rel="external nofollow" class="btn btn-info">修改</a></td> 此處的?id可以改成 ?iid,或者其他的名稱,在views.py文件里對函數(shù)edit_book修改即可edit_id=request.GET.get('iid')
</tr> {% endfor %} </tbody> </table> </div> </div> </div> </body> </html>
主頁:

之后,根據(jù)不同的操作指向不同的頁面,這部分功能需要修改urls.py
from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
# url(r'^admin/', admin.site.urls),
url(r'^home/',views.home),
url(r'^index/',views.index),
url(r'^login/',views.login),
url(r'^book_list/',views.book_list),
#添加新書
url('^add_book/',views.add_book),
#刪除書籍
url('^delete_book/',views.delete_book),
#修改書籍
url(r'^edit_book/',views.edit_book),
]其次,不同操作指向不同的頁面
add_book.html
主要的部分
<form class="form-horizontal" action="/add_book/" method="post"> #提交到 add_book <div class="form-group"> <label for="inputbookname" class="col-sm-2 control-label">書籍名稱</label> <div class="col-sm-3"> <input type="text" class="form-control" id="inputbookname" name="book_name"> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">添加新書</button>
edit_book.html
主要部分
<form class="form-horizontal" action="/edit_book/" method="post">
<input hidden type="text" name="book_id" value="{{ book.id }}">
<div class="form-group">
<label for="inputbookname" class="col-sm-2 control-label">書籍名稱</label>
<div class="col-sm-3">
<input type="text" class="form-control" id="inputbookname" name="book_name" value="{{ book.title }}">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">提交修改</button>刪除在后臺執(zhí)行
最后后臺函數(shù)的配置views.py
def book_list(request):
#找到所有的書
books=models.Book.objects.all()
return render(request,"book_list.html",{"book_list":books})
def add_book(request):
#判斷是否為post
if request.method=="POST":
new_book_name=request.POST.get("book_name")
#去數(shù)據(jù)庫創(chuàng)建一條記錄
models.Book.objects.create(title=new_book_name)
#跳轉(zhuǎn)回之前書籍展示的頁面
return redirect("/book_list/")
#返回一個頁面讓用戶填寫新書的相關(guān)信息
return render(request,"add_book.html")
def delete_book(request):
#取到要刪除書的id,如何從get請求獲取數(shù)據(jù)
delete_id=request.GET.get("id")
#根據(jù)id值去數(shù)據(jù)庫取對應(yīng)的數(shù)據(jù)
models.Book.objects.get(id=delete_id).delete()
return redirect("/book_list/")
def edit_book(request):
if request.method=="POST":
#取到書的id
book_id=request.POST.get("book_id")
#用戶修改后的名稱
new_book_title=request.POST.get("book_name")
#在數(shù)據(jù)庫中查找id對應(yīng)的記錄
book_obj= models.Book.objects.get(id=book_id)
#將用戶的名稱給修改到這個id中
book_obj.title=new_book_title
#保存提交
book_obj.save()
#跳轉(zhuǎn)到書列表的頁面
return redirect("/book_list/")
edit_id=request.GET.get('id')
book=models.Book.objects.get(id=edit_id)
return render(request,"edit_book.html",{"book":book}) #以字典的方式傳遞變量
#note:
# 對書籍進(jìn)行編輯,是通過book_list頁面?zhèn)鬟fid(或者iid),在對上面的函數(shù)獲取其id時得到edit_id,知道其id和title就可以進(jìn)行修改關(guān)于使用django框架怎么實(shí)現(xiàn)單表增刪改操作就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
本文題目:使用django框架怎么實(shí)現(xiàn)單表增刪改操作-創(chuàng)新互聯(lián)
網(wǎng)頁路徑:http://www.yijiale78.com/article42/dpedec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、外貿(mào)建站、品牌網(wǎng)站制作、服務(wù)器托管、網(wǎng)頁設(shè)計(jì)公司、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容