溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用django框架怎么實現(xiàn)單表增刪改操作

發(fā)布時間:2021-05-26 11:09:00 來源:億速云 閱讀:148 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章給大家介紹使用django框架怎么實現(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>

使用django框架怎么實現(xiàn)單表增刪改操作

主頁:

使用django框架怎么實現(xiàn)單表增刪改操作

之后,根據(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框架怎么實現(xiàn)單表增刪改操作就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI