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

django的ORM操作 增加和查詢

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

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

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

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

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

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

ORM 就是封裝了SQ語句,給對(duì)象進(jìn)行增刪改查,實(shí)現(xiàn)對(duì)數(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對(duì)數(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)

命令臺(tái)執(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è)對(duì)象,
      <tr>

          <td>{{ book.id }}</td> #---把每個(gè)對(duì)象的屬性的值渲染出來,
          <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ù)庫,再?gòu)臄?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里的字段名,對(duì)應(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里的字段名,對(duì)應(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')

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

相關(guān)文章

  • python中argparse模塊及action='store_true'詳解

    python中argparse模塊及action='store_true'詳解

    argparse?是一個(gè)用來解析命令行參數(shù)的?Python?庫,它是?Python?標(biāo)準(zhǔn)庫的一部分,這篇文章主要介紹了python中argparse模塊及action=‘store_true‘詳解,需要的朋友可以參考下
    2023-02-02
  • Python實(shí)現(xiàn)對(duì)數(shù)坐標(biāo)系繪制與自定義映射

    Python實(shí)現(xiàn)對(duì)數(shù)坐標(biāo)系繪制與自定義映射

    這篇文章主要為大家學(xué)習(xí)介紹了如何利用Python實(shí)現(xiàn)對(duì)數(shù)坐標(biāo)系繪制與坐標(biāo)自定義映射,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2023-08-08
  • pytest利用request?fixture實(shí)現(xiàn)個(gè)性化測(cè)試需求詳解

    pytest利用request?fixture實(shí)現(xiàn)個(gè)性化測(cè)試需求詳解

    這篇文章主要為大家詳細(xì)介紹了pytest如何利用request?fixture實(shí)現(xiàn)個(gè)性化測(cè)試需求,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下
    2023-09-09
  • Python中?whl包、tar.gz包的區(qū)別詳解

    Python中?whl包、tar.gz包的區(qū)別詳解

    whl格式本質(zhì)上是一個(gè)壓縮包,里面包含了py文件,以及經(jīng)過編譯的pyd文件,這篇文章主要介紹了Python中?whl包、tar.gz包的區(qū)別,需要的朋友可以參考下
    2022-08-08
  • pycharm中jupyter的使用圖文教程

    pycharm中jupyter的使用圖文教程

    這篇文章主要介紹了pycharm中jupyter的使用圖文教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • OpenCV-Python直方圖均衡化實(shí)現(xiàn)圖像去霧

    OpenCV-Python直方圖均衡化實(shí)現(xiàn)圖像去霧

    直方圖均衡化可以達(dá)到增強(qiáng)圖像顯示效果的目的。最常用的比如去霧。本文就來實(shí)現(xiàn)直方圖均衡化實(shí)現(xiàn)圖像去霧,感興趣的可以了解一下
    2021-06-06
  • Django的CVB實(shí)例詳解

    Django的CVB實(shí)例詳解

    在本篇文章小編給大家整理的是關(guān)于Django的CVB實(shí)例詳解內(nèi)容,有需要的朋友們可以跟著學(xué)習(xí)下。
    2020-02-02
  • python裝飾器深入學(xué)習(xí)

    python裝飾器深入學(xué)習(xí)

    這篇文章主要深入學(xué)習(xí)了python裝飾器的相關(guān)資料,什么是裝飾器?裝飾器遵循的原則等,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • Python?defaultdict教程示例詳解

    Python?defaultdict教程示例詳解

    這篇文章主要為大家介紹了Python?defaultdict教程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • Python 普通最小二乘法(OLS)進(jìn)行多項(xiàng)式擬合的方法

    Python 普通最小二乘法(OLS)進(jìn)行多項(xiàng)式擬合的方法

    今天小編就為大家分享一篇Python 普通最小二乘法(OLS)進(jìn)行多項(xiàng)式擬合的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12

最新評(píng)論

福安市| 托克逊县| 上思县| 绩溪县| 历史| 西藏| 贞丰县| 图木舒克市| 宽甸| 岳普湖县| 定日县| 西昌市| 肥东县| 灵武市| 淄博市| 霸州市| 宜昌市| 洱源县| 龙南县| 弥渡县| 凉城县| 遂宁市| 沙田区| 哈尔滨市| 沾益县| 汉沽区| 即墨市| 福海县| 泌阳县| 桓仁| 富锦市| 望城县| 永寿县| 安多县| 三原县| 三江| 成安县| 陵川县| 嵊泗县| 义乌市| 新郑市|