最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

django的ORM操作 刪除和編輯實(shí)現(xiàn)詳解

 更新時(shí)間:2019年07月24日 15:30:23   作者:谷子的  
這篇文章主要介紹了django的ORM操作 刪除和編輯實(shí)現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

向server端傳送數(shù)據(jù)

有2中方法,1 是 通過url 地址, 2 是通過路徑

向server端傳參數(shù)方式

1,通過數(shù)據(jù) http://127.0.0.1:8000/blog/?id=2

2, 通過路徑 http://17.0.0.1:8000/blog/20

# url(r'blog/(\d{4})')

刪除功能:

在url文件中,創(chuàng)建一個(gè)delbook路徑, 通過url的地址拿到id實(shí)現(xiàn)刪除

urlpatterns = [
  url(r'^admin/', admin.site.urls),
  url(r'^$',views.index),#指定一個(gè)根目錄,也指向index頁面
  url(r'^index/$',views.index),
  url(r'^addbook/$',views.addbook),
  url(r'^delbook/$',views.delbook), -------------------------------------------del刪除功能,對(duì)應(yīng)視圖函數(shù)
  #(\d+)分組后,作為參數(shù)傳給editorbook函數(shù),editorbook(request,1或2 等等)
  url(r'^editorbook/(\d+)',views.editorbook),
]

在index.html 頁面中,點(diǎn)擊刪除按鈕,在href 加上?id={{ book.id}}要?jiǎng)h除的書籍,

在get請(qǐng)求時(shí),url加上刪除時(shí)點(diǎn)擊到的id,獲取id,就可以刪除#}

<a href="/delbook/?id={{ book.id }}" rel="external nofollow" rel="external nofollow" ><button class="btn btn-primary">刪除</button></a>

在刪除一條記錄后,頁面的順序是錯(cuò)亂,在前端顯示的是數(shù)據(jù)庫(kù)的id,用forloop.counter 默認(rèn)從1開始循環(huán)顯示,與數(shù)據(jù)庫(kù)的id無關(guān),

<td>{{ forloop.counter }}</td>

{% 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/' %}" rel="external nofollow" >

  <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>價(jià)格</th>
        <th>出版日期</th>
        <th>作者</th>
        <th>出版社</th>
        <th>分類</th>
        <th>操作</th>
      </tr>

       {% for book in book_list %}
      <tr>

{# 在前端顯示的是數(shù)據(jù)庫(kù)的id,用forloop.counter 默認(rèn)從1開始循環(huán)顯示,與數(shù)據(jù)庫(kù)的id無關(guān),   <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>
{#            當(dāng)前的ip和端口都可以省略,會(huì)自動(dòng)添加,a標(biāo)簽會(huì)訪問addbook路徑#}
             <a href="/addbook/" rel="external nofollow" ><button class="btn btn-primary">添加</button></a>
{#             在get請(qǐng)求時(shí),url加上刪除時(shí)點(diǎn)擊到的id,獲取id,就可以刪除#}
             <a href="/delbook/?id={{ book.id }}" rel="external nofollow" rel="external nofollow" ><button class="btn btn-primary">刪除</button></a>
{#             取到路徑,#}
            <a href="/editorbook/{{ book.id }}" rel="external nofollow" ><button class="btn btn-primary">編輯</button></a>
          </td>d

      </tr>
      {% endfor %}
    </table>
  </div>  
 </div>
</div>
</body>
<script>
</script>
</html>

在views文件中,編輯delbook函數(shù),

django里的刪除和編輯,前提都是 要先找到,利用filter()方法,條件是id或者是name,等都可以,

步驟1,用get的方法,從url路徑中拿到id,

步驟2,對(duì)數(shù)據(jù)庫(kù)的id和要url里獲取到的id,對(duì)應(yīng),就執(zhí)行delete()方法,就刪除指定的記錄,數(shù)據(jù)庫(kù)也會(huì)減少一條記錄,

#刪除和修改,都是要先找到記錄(對(duì)象)
def delbook(request):

  #先過濾,加上過濾的條件,然后用delete()
  #向server端傳參數(shù)方式
  #1,通過數(shù)據(jù) 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/')

=======

ORM的編輯功能

在url文件中創(chuàng)建editorbook路徑,和映射到視圖函數(shù),

通過訪問的路徑,拿到id,

urlpatterns = [
  url(r'^admin/', admin.site.urls),
  url(r'^$',views.index),#指定一個(gè)根目錄,也指向index頁面
  url(r'^index/$',views.index),
  url(r'^addbook/$',views.addbook),
  url(r'^delbook/$',views.delbook), -------------------------------------------del刪除功能,對(duì)應(yīng)視圖函數(shù)
  #(\d+)分組后,作為參數(shù)傳給editorbook函數(shù),editorbook(request,1或2 等等)
  url(r'^editorbook/(\d+)',views.editorbook),-----------------------------editorbook 編輯功能,對(duì)應(yīng)一個(gè)視圖函數(shù)
]

編輯一個(gè)editorbook頁面,

編輯時(shí)要獲取要編輯的是哪個(gè)對(duì)象,

比如: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/' %} " rel="external nofollow" >

  <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的方法提交數(shù)據(jù),走到editbook 視圖函數(shù)#}
      <form class=editorbook" action="/editorbook/{{ book_obj.id }}/" method="post"> -----------加上從數(shù)據(jù)庫(kù)里拿到的id號(hào),

{#      當(dāng)點(diǎn)擊添加按鈕時(shí),是執(zhí)行視圖函數(shù),在數(shù)據(jù)庫(kù)中添加一條記錄,然后再把這個(gè)記錄添加到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">價(jià)格:</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函數(shù),

表單的提交是post 方法,用post的方法獲取到每個(gè)輸入框的值, 然后保存到數(shù)據(jù)庫(kù)

def editorbook(request,id):

# 2, 通過路徑 http://17.0.0.1:8000/blog/20
# url(r'blog/(\d{4})')
#通過id獲取要修改的對(duì)象,在前端中把對(duì)象的每個(gè)字段屬性給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修改并保存數(shù)據(jù) ,name 是數(shù)據(jù)庫(kù)的字段,bookname是前端form表單里 的name屬性值,把輸入框里獲取到的值給數(shù)據(jù)庫(kù)的字段進(jìn)行保存,
    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})

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python開發(fā)微信服務(wù)號(hào)消息推送示例

    python開發(fā)微信服務(wù)號(hào)消息推送示例

    這篇文章主要為大家介紹了python開發(fā)微信服務(wù)號(hào)消息推送示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • 使用python腳本實(shí)現(xiàn)查詢火車票工具

    使用python腳本實(shí)現(xiàn)查詢火車票工具

    這篇文章主要介紹了使用python腳本實(shí)現(xiàn)查詢火車票工具,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-07-07
  • 詳解Python3除法之真除法、截?cái)喑ê拖氯≌麑?duì)比

    詳解Python3除法之真除法、截?cái)喑ê拖氯≌麑?duì)比

    這篇文章主要介紹了詳解Python3除法之真除法、截?cái)喑ê拖氯≌麑?duì)比,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • Python學(xué)習(xí)筆記之變量與轉(zhuǎn)義符

    Python學(xué)習(xí)筆記之變量與轉(zhuǎn)義符

    這篇文章主要介紹了Python學(xué)習(xí)筆記之變量與轉(zhuǎn)義符,本文從零開始學(xué)習(xí)Python,知識(shí)點(diǎn)很細(xì),有共同目標(biāo)的小伙伴可以一起來學(xué)習(xí)
    2023-03-03
  • Pyhacker實(shí)現(xiàn)端口掃描器

    Pyhacker實(shí)現(xiàn)端口掃描器

    這篇文章主要為大家介紹了Pyhacker實(shí)現(xiàn)端口掃描器的過程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • python使用pandas處理excel文件轉(zhuǎn)為csv文件的方法示例

    python使用pandas處理excel文件轉(zhuǎn)為csv文件的方法示例

    這篇文章主要介紹了python使用pandas處理excel文件轉(zhuǎn)為csv文件的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Python3爬蟲學(xué)習(xí)之MySQL數(shù)據(jù)庫(kù)存儲(chǔ)爬取的信息詳解

    Python3爬蟲學(xué)習(xí)之MySQL數(shù)據(jù)庫(kù)存儲(chǔ)爬取的信息詳解

    這篇文章主要介紹了Python3爬蟲學(xué)習(xí)之MySQL數(shù)據(jù)庫(kù)存儲(chǔ)爬取的信息,涉及Python3針對(duì)mysql數(shù)據(jù)庫(kù)的連接、信息存儲(chǔ)等相關(guān)操作技巧,需要的朋友可以參考下
    2018-12-12
  • python實(shí)現(xiàn)防截圖的6種方法詳解

    python實(shí)現(xiàn)防截圖的6種方法詳解

    防截圖是指一組技術(shù)或方法,用于防止他人在未經(jīng)允許的情況下在屏幕上截取或記錄圖像,這是一個(gè)重要的安全措施,它可以防止竊取敏感信息或監(jiān)視個(gè)人信息,本文為大家整理了6種python可以防截圖的方法,需要的可以參考下
    2023-10-10
  • Python Socket編程詳細(xì)介紹

    Python Socket編程詳細(xì)介紹

    這篇文章主要介紹了Python Socket編程詳細(xì)介紹,socket可以建立連接,傳遞數(shù)據(jù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-03-03
  • Python如何批量生成和調(diào)用變量

    Python如何批量生成和調(diào)用變量

    這篇文章主要介紹了Python如何批量生成和調(diào)用變量,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下
    2020-11-11

最新評(píng)論

苗栗县| 南充市| 江口县| 汝州市| 中西区| 阳朔县| 永仁县| 营口市| 营口市| 铜川市| 香港| 延长县| 罗田县| 景德镇市| 塘沽区| 太白县| 日照市| 延长县| 武夷山市| 河北区| 获嘉县| 囊谦县| 武宣县| 金寨县| 建德市| 嘉兴市| 平塘县| 阿荣旗| 义乌市| 柳江县| 彰化市| 田阳县| 江达县| 峨眉山市| 永宁县| 奉节县| 潮安县| 垫江县| 瓦房店市| 福海县| 鹿邑县|