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

django的ORM操作 增加和查詢

 更新時(shí)間:2019年07月26日 09:39:04   作者:谷子的  
這篇文章主要介紹了django的ORM操作 增加和查詢,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

ORM 對象關(guān)系映射

在數(shù)據(jù)庫中,實(shí)現(xiàn)對數(shù)據(jù)的增刪改查,使用的是SQ語句,

在django中,通過python代碼,實(shí)現(xiàn)對數(shù)據(jù)庫的增刪改查,這就是ORM。

在python中,用類名 代表 django數(shù)據(jù)庫的表名,

用對象 ,代表django數(shù)據(jù)庫的一條記錄,

ORM 就是封裝了SQ語句,給對象進(jìn)行增刪改查,實(shí)現(xiàn)對數(shù)據(jù)庫的操作,

在settings 文件中,默認(rèn)了splite的數(shù)據(jù)庫,自己可以修改

DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
  }
}<br data-filtered="filtered"><br data-filtered="filtered">BASE_DIR 是代表當(dāng)前的文件夾 settings,

django對數(shù)據(jù)庫的遷移,只需要修改配置即可,

===

在model文件中,設(shè)計(jì)表,,

from django.db import models

# Create your models here.

#繼承來自models ,
class Book(models.Model):
  name = models.CharField(max_length=32)
  price = models.IntegerField()
  Date = models.DateField()
  auth = models.CharField(max_length=32)
  publish = models.CharField(max_length=32)

命令臺執(zhí)行命令。

python manage.py makemigrations 
python manage.oy migrate 

生成數(shù)據(jù)庫表

在template ,寫一個(gè)主頁index.html文件,用于顯示添加后書籍

{% load staticfiles %} #  ------引用靜態(tài)文件
<!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" >  # ---導(dǎo)入bootstrap 文件

  <style>

    .container{
      margin-top: 50px;
    }

  </style>

</head>
<body>


<div class="container">
  <div class="row">


    <div class="col-md-6 col-md-offset-2">
    <a href="/addbook/" rel="external nofollow" ><button class="btn btn-primary">添加書籍</button></a>
    <table class="table table-striped">
      <tr>
        <th>ID</th>
        <th>書名</th>
        <th>價(jià)格</th>
        <th>出版日期</th>
        <th>作者</th>
        <th>出版社</th>
      </tr>
{#      <tr>#}
{#        <td>1</td>#}
{#        <td>水滸城</td>#}
{#        <td>110</td>#}
{#        <td>2011.1.1</td>#}
{##}
{#        <td>egon</td>#}
{#        <td>人民出版社</td>#}
{#      </tr>#}
{#       <tr>#}
{#        <td>2</td>#}
{#        <td>水滸城</td>#}
{#        <td>110</td>#}
{#        <td>2011.1.1</td>#}
{##}
{#        <td>egon</td>#}
{#        <td>人民出版社</td>#}
{#      </tr>#}
       {% for book in book_list %}  #--從數(shù)據(jù)庫取到的數(shù)據(jù)是一個(gè)QuerySet集合,進(jìn)行for循環(huán),遍歷出每個(gè)對象,
      <tr>

          <td>{{ book.id }}</td> #---把每個(gè)對象的屬性的值渲染出來,
          <td>{{ book.name }}</td>
          <td>{{ book.price }}</td>
          <td>{{ book.Date }}</td>
          <td>{{ book.auth }}</td>
          <td>{{ book.publish }}</td>

      </tr>
      {% endfor %}
    </table>

  </div>
  </div>

</div>
</body>

<script>


</script>

</html>

寫一個(gè)addbook.html 文件,用于添加書籍,然后跳轉(zhuǎn)到index頁面

{% 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 }}#}
      <form class=addbook" action="/addbook/" method="post">

{#      當(dāng)點(diǎn)擊添加按鈕時(shí),是執(zhí)行視圖函數(shù),在數(shù)據(jù)庫中添加一條記錄,然后再把這個(gè)記錄添加到index頁面#}

      <div class="form-group">
        <label for="bookname">書名:</label>
        <input type="text" class="form-control" id="bookname" name="bookname">  #---input 中的name屬性,用于獲取輸入的值,
      </div>
      <div class="form-group">
        <label for="price">價(jià)格:</label>
        <input type="text" class="form-control" id="price" name="price"> #---input 中的name屬性,用于獲取輸入的值,
      </div>
      <div class="form-group">
        <label for="Date">日期:</label>
        <input type="text" class="form-control" id="Date" name="Date">  #---input 中的name屬性,用于獲取輸入的值,
      </div>
      <div class="form-group">
        <label for="auth">作者:</label>
        <input type="text" class="form-control" id="auth" name="auth">  #---input 中的name屬性,用于獲取輸入的值,
      </div>

      <div class="form-group">
        <label for="publish">出版社:</label>
        <input type="text" class="form-control" id="publish" name="publish">  #---input 中的name屬性,用于獲取輸入的值,
      </div>

      <input class="btn btn-info" type="submit" value='添加'>

      </form>



    </div>
  </div>

</div>


</body>

</html>

在url文件中,匹配index頁面和addbook頁面的視圖函數(shù)

from django.conf.urls import url
from django.contrib import admin

from app01 import views

urlpatterns = [
  url(r'^admin/', admin.site.urls),
  url(r'^index/$',views.index),
  url(r'^addbook/$',views.addbook),
]

在views文件中,寫邏輯代碼。用戶訪問index頁面,點(diǎn)擊添加按鈕,跳到addbook頁面添加數(shù)據(jù)后,提交后,存到數(shù)據(jù)庫,再從數(shù)據(jù)庫拿到數(shù)據(jù)顯示到index頁面

步驟1,在index頁面要顯示數(shù)據(jù)庫的信息,就要導(dǎo)入Book表,獲取所有的數(shù)據(jù),return render 進(jìn)行渲染到頁面展示

步驟2 ,addbook函數(shù),從form表單中拿到添加的數(shù)據(jù),從request里,以post的方法拿到,

存到數(shù)據(jù)庫,用create方法,表名.objects .create(數(shù)據(jù)庫的字段名= 表單中的name的屬性名)

Book.objects.create(name = bookname,price = price, Date = Date, auth = auth , publish = publish)

左邊的name 是models里的字段名,對應(yīng)到form里取到的值,

from django.shortcuts import render,redirect

# Create your views here.
from app01.models import Book

def index(request):

  #把數(shù)據(jù)庫的數(shù)據(jù)嵌入到頁面進(jìn)行顯示
  #查詢api,所有書籍
  book_list = Book.objects.all() #數(shù)據(jù)類型是QuerySet:[book1,book2...]

  return render(request,'index.html',locals())


def addbook(request):

  #是form提交post的方法,
  if request.method == 'POST':
    #從form表單取數(shù)據(jù)
    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')

  #把數(shù)據(jù)存到數(shù)據(jù)庫,先把models 中的表引導(dǎo)到views文件

  #方法1,左邊的name 是models里的字段名,對應(yīng)到form里取到的值,


    Book.objects.create(name = bookname,price = price, Date = Date, auth = auth , publish = publish)


  #存到數(shù)據(jù)庫后要放到index頁面,但現(xiàn)在是addbook頁面,所以要跳轉(zhuǎn)

    return redirect('/index/')

  return render(request,'addbook.html')

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

相關(guān)文章

  • python使用ctypes調(diào)用dll遇到的坑解決記錄

    python使用ctypes調(diào)用dll遇到的坑解決記錄

    這篇文章主要為大家介紹了python使用ctypes調(diào)用dll遇到的坑解決記錄,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • Python如何繪制日歷圖和熱力圖

    Python如何繪制日歷圖和熱力圖

    這篇文章主要介紹了Python如何繪制日歷圖和熱力圖,幫助大家更好的理解和學(xué)習(xí)Python,感興趣的朋友可以了解下
    2020-08-08
  • Python實(shí)現(xiàn)隱馬爾可夫模型的前向后向算法的示例代碼

    Python實(shí)現(xiàn)隱馬爾可夫模型的前向后向算法的示例代碼

    這篇文章主要介紹了Python實(shí)現(xiàn)隱馬爾可夫模型的前向后向算法,本文通過實(shí)例代碼給大家講解的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Python 根據(jù)相鄰關(guān)系還原數(shù)組的兩種方式(單向構(gòu)造和雙向構(gòu)造)

    Python 根據(jù)相鄰關(guān)系還原數(shù)組的兩種方式(單向構(gòu)造和雙向構(gòu)造)

    本文主要介紹了Python 根據(jù)相鄰關(guān)系還原數(shù)組的兩種方式,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • python之matplotlib學(xué)習(xí)繪制動態(tài)更新圖實(shí)例代碼

    python之matplotlib學(xué)習(xí)繪制動態(tài)更新圖實(shí)例代碼

    這篇文章主要介紹了python之matplotlib學(xué)習(xí)繪制動態(tài)更新圖實(shí)例代碼,文中涉及具體實(shí)現(xiàn)代碼,演示效果及運(yùn)行時(shí)出現(xiàn)的問題分析等相關(guān)內(nèi)容,小編覺得還是挺不錯的,這里分享給大家,需要的朋友可以參考下
    2018-01-01
  • 使用PyCharm安裝pytest及requests的問題

    使用PyCharm安裝pytest及requests的問題

    這篇文章主要介紹了使用PyCharm安裝pytest及requests的相關(guān)資料,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • python根據(jù)照片獲取地理位置及泄露防御

    python根據(jù)照片獲取地理位置及泄露防御

    這篇文章主要為大家介紹了python根據(jù)照片獲取地理位置及泄露防御,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • Python 從相對路徑下import的方法

    Python 從相對路徑下import的方法

    今天小編就為大家分享一篇Python 從相對路徑下import的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • Numpy的各種下標(biāo)操作的示例代碼

    Numpy的各種下標(biāo)操作的示例代碼

    本文主要介紹了Numpy的各種下標(biāo)操作的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Python全棧之模板渲染詳解

    Python全棧之模板渲染詳解

    這篇文章主要為大家詳細(xì)介紹了Python全棧之模板渲染,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02

最新評論