django中ORM操作實現(xiàn)增加和查詢的示例分析-創(chuàng)新互聯(lián)

這篇文章主要介紹django中ORM操作實現(xiàn)增加和查詢的示例分析,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

成都創(chuàng)新互聯(lián)長期為近1000家客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為珠山企業(yè)提供專業(yè)的網(wǎng)站設(shè)計、網(wǎng)站制作,珠山網(wǎng)站改版等技術(shù)服務(wù)。擁有十年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。

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

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

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

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

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

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

在settings 文件中,默認了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 是代表當前的文件夾 settings,

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

===

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

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 ,寫一個主頁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>價格</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ù)是一個QuerySet集合,進行for循環(huán),遍歷出每個對象,
      <tr>

          <td>{{ book.id }}</td> #---把每個對象的屬性的值渲染出來,
          <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>

寫一個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">

{#      當點擊添加按鈕時,是執(zhí)行視圖函數(shù),在數(shù)據(jù)庫中添加一條記錄,然后再把這個記錄添加到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">價格:</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頁面,點擊添加按鈕,跳到addbook頁面添加數(shù)據(jù)后,提交后,存到數(shù)據(jù)庫,再從數(shù)據(jù)庫拿到數(shù)據(jù)顯示到index頁面

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

步驟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ù)嵌入到頁面進行顯示
  #查詢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')

以上是“django中ORM操作實現(xiàn)增加和查詢的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計公司行業(yè)資訊頻道!

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

網(wǎng)頁標題:django中ORM操作實現(xiàn)增加和查詢的示例分析-創(chuàng)新互聯(lián)
新聞來源:http://bm7419.com/article16/cedsgg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站軟件開發(fā)、微信小程序、微信公眾號網(wǎng)站策劃、響應(yīng)式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化