成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

使用django框架怎么實(shí)現(xiàn)單表增刪改操作-創(chuàng)新互聯(lián)

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

創(chuàng)新互聯(lián)基于成都重慶香港及美國(guó)等地區(qū)分布式IDC機(jī)房數(shù)據(jù)中心構(gòu)建的電信大帶寬,聯(lián)通大帶寬,移動(dòng)大帶寬,多線BGP大帶寬租用,是為眾多客戶提供專業(yè)成都天府聯(lián)通服務(wù)器托管報(bào)價(jià),主機(jī)托管價(jià)格性價(jià)比高,為金融證券行業(yè)服務(wù)器托管,ai人工智能服務(wù)器托管提供bgp線路100M獨(dú)享,G口帶寬及機(jī)柜租用的專業(yè)成都idc公司。

代碼如下

<!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文件里對(duì)函數(shù)edit_book修改即可edit_id=request.GET.get('iid')
 </tr> {% endfor %} </tbody> </table> </div> </div> </div> </body> </html>

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

主頁:

使用django框架怎么實(shí)現(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>

刪除在后臺(tái)執(zhí)行

最后后臺(tái)函數(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/")
  #返回一個(gè)頁面讓用戶填寫新書的相關(guān)信息
  return render(request,"add_book.html")
def delete_book(request):
  #取到要?jiǎng)h除書的id,如何從get請(qǐng)求獲取數(shù)據(jù)
  delete_id=request.GET.get("id")
  #根據(jù)id值去數(shù)據(jù)庫取對(duì)應(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對(duì)應(yīng)的記錄
    book_obj= models.Book.objects.get(id=book_id)
    #將用戶的名稱給修改到這個(gè)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:
# 對(duì)書籍進(jìn)行編輯,是通過book_list頁面?zhèn)鬟fid(或者iid),在對(duì)上面的函數(shù)獲取其id時(shí)得到edit_id,知道其id和title就可以進(jìn)行修改

關(guān)于使用django框架怎么實(shí)現(xiàn)單表增刪改操作就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

本文題目:使用django框架怎么實(shí)現(xiàn)單表增刪改操作-創(chuàng)新互聯(lián)
網(wǎng)頁路徑:http://jinyejixie.com/article42/dpedec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(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)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站
宁化县| 固始县| 全椒县| 湘西| 西峡县| 临洮县| 阿瓦提县| 呼玛县| 闽清县| 周宁县| 临武县| 晋江市| 庆城县| 家居| 海兴县| 临江市| 门源| 腾冲县| 衡阳县| 突泉县| 独山县| 永安市| 贡嘎县| 青冈县| 阳朔县| 阿合奇县| 从化市| 精河县| 海盐县| 宜城市| 镇原县| 岳普湖县| 和林格尔县| 新竹市| 体育| 横峰县| 丰都县| 金秀| 买车| 尼勒克县| 涞源县|