欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

django框架單表操作之增刪改實(shí)例分析

 更新時(shí)間:2019年12月16日 10:24:49   作者:dawn-liu  
這篇文章主要介紹了django框架單表操作之增刪改,結(jié)合實(shí)例形式分析了Django框架前臺(tái)數(shù)據(jù)操作提交與后臺(tái)處理相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了django框架單表操作之增刪改。分享給大家供大家參考,具體如下:

首先找到操作的首頁(yè)面

代碼如下

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css" rel="external nofollow" >
  <title>書(shū)列表</title>
</head>
<body>
<div class="container">
  <a href="/add_book/" rel="external nofollow" class="btn btn-success">添加新書(shū)</a>
  <div class="panel panel-primary">
    <div class="panel-heading">書(shū)籍管理</div>
    <div class="panel-body">
      <table class="table table-bordered table-striped">
        <thead>
        <tr>
          <th>#</th>
          <th>書(shū)名</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>

主頁(yè):

之后,根據(jù)不同的操作指向不同的頁(yè)面,這部分功能需要修改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),
  #添加新書(shū)
  url('^add_book/',views.add_book),
  #刪除書(shū)籍
  url('^delete_book/',views.delete_book),
  #修改書(shū)籍
  url(r'^edit_book/',views.edit_book),
]

其次,不同操作指向不同的頁(yè)面

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">書(shū)籍名稱</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">添加新書(shū)</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">書(shū)籍名稱</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):
  #找到所有的書(shū)
  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ù)庫(kù)創(chuàng)建一條記錄
    models.Book.objects.create(title=new_book_name)
    #跳轉(zhuǎn)回之前書(shū)籍展示的頁(yè)面
    return redirect("/book_list/")
  #返回一個(gè)頁(yè)面讓用戶填寫(xiě)新書(shū)的相關(guān)信息
  return render(request,"add_book.html")
def delete_book(request):
  #取到要?jiǎng)h除書(shū)的id,如何從get請(qǐng)求獲取數(shù)據(jù)
  delete_id=request.GET.get("id")
  #根據(jù)id值去數(shù)據(jù)庫(kù)取對(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":
    #取到書(shū)的id
    book_id=request.POST.get("book_id")
    #用戶修改后的名稱
    new_book_title=request.POST.get("book_name")
    #在數(shù)據(jù)庫(kù)中查找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)到書(shū)列表的頁(yè)面
    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ì)書(shū)籍進(jìn)行編輯,是通過(guò)book_list頁(yè)面?zhèn)鬟fid(或者iid),在對(duì)上面的函數(shù)獲取其id時(shí)得到edit_id,知道其id和title就可以進(jìn)行修改

希望本文所述對(duì)大家基于Django框架的Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論