django的ORM操作 增加和查詢
ORM 對象關系映射
在數(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文件中,設計表,,
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" > # ---導入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 文件,用于添加書籍,然后跳轉到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ù)庫的信息,就要導入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里的字段名,對應到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 中的表引導到views文件
#方法1,左邊的name 是models里的字段名,對應到form里取到的值,
Book.objects.create(name = bookname,price = price, Date = Date, auth = auth , publish = publish)
#存到數(shù)據(jù)庫后要放到index頁面,但現(xiàn)在是addbook頁面,所以要跳轉
return redirect('/index/')
return render(request,'addbook.html')
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
python使用ctypes調(diào)用dll遇到的坑解決記錄
這篇文章主要為大家介紹了python使用ctypes調(diào)用dll遇到的坑解決記錄,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12
Python實現(xiàn)隱馬爾可夫模型的前向后向算法的示例代碼
這篇文章主要介紹了Python實現(xiàn)隱馬爾可夫模型的前向后向算法,本文通過實例代碼給大家講解的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12
Python 根據(jù)相鄰關系還原數(shù)組的兩種方式(單向構造和雙向構造)
本文主要介紹了Python 根據(jù)相鄰關系還原數(shù)組的兩種方式,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07
python之matplotlib學習繪制動態(tài)更新圖實例代碼
這篇文章主要介紹了python之matplotlib學習繪制動態(tài)更新圖實例代碼,文中涉及具體實現(xiàn)代碼,演示效果及運行時出現(xiàn)的問題分析等相關內(nèi)容,小編覺得還是挺不錯的,這里分享給大家,需要的朋友可以參考下2018-01-01

