Python - Django - ORM 實例(二)
阿新 • • 發佈:2019-04-26
cut 獲得 put con 修改 thead 不存在 技術分享 cts
在 app01/models.py 中添加 Book 類對象表
from django.db import models # Create your models here. # 出版社 class Publisher(models.Model): id = models.AutoField(primary_key=True) # 自增的 ID 主鍵 # 創建一個 varchar(64) 的唯一的不為空的字段 name = models.CharField(max_length=64, null=False, unique=True) # 書籍 class Book(models.Model): id = models.AutoField(primary_key=True) # 自增的 ID 主鍵 # 創建一個 varchar(64) 的唯一的不為空的字段 title = models.CharField(max_length=64, null=False, unique=True) # 和出版社關聯的外鍵字段 publisher = models.ForeignKey(to="Publisher")
然後執行命令更新到數據庫中
[email protected] > makemigrations [email protected] > migrate
在 Book 表中添加 3 條數據
展示書籍列表:
創建 book_list.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>書籍列表</title> </head> <body> <table border="1"> <thead> <tr> <th>id</th> <th>書名</th> <th>出版社</th> </tr> </thead> <tbody> {% for book in book_list %} <tr> <td>{{ book.id }}</td> <td>{{ book.title }}</td> <td>{{ book.publisher.name }}</td> </tr> {% endfor %} </tbody> </table> </body> </html>
這裏的 book.publisher 獲取到的是 Publisher 對象,因為 publisher 關聯了 Publisher 對象
在 app01/views.py 中添加 book_list 函數
from django.shortcuts import render, redirect, HttpResponse from app01 import models # 展示出版社列表 def publisher_list(request): # 去數據庫查出所有的出版社,填充到 html 中,返回給用戶 ret = models.Publisher.objects.all().order_by("id") # order_by("id") 通過 id 進行排序 return render(request, "publisher_list.html", {"publisher_list": ret}) # 添加新的出版社 def add_publisher(request): # 如果是 POST 請求,就獲取用戶填寫的數據 if request.method == "POST": new_publisher = request.POST.get("publisher_name") # 獲得數據後去數據庫中新增一條數據 models.Publisher.objects.create(name=new_publisher) # 添加成功後進行跳轉 return redirect("/publisher_list/") # 用戶來到該界面返回的 html 頁面 return render(request, "add_publisher.html") # 刪除出版社 def del_publisher(request): # 從 GET 請求的參數中拿到要刪除的 id 值 del_id = request.GET.get(‘id‘) # 如果取到 id 值,就去數據庫中刪除該 id 的數據 if del_id: # 根據 id 查找數據,並刪除 del_obj = models.Publisher.objects.get(id=del_id).delete() # 刪除後返回頁面 return redirect("/publisher_list/") else: return HttpResponse("要刪除的數據不存在!") # 編輯出版社 def edit_publisher(request): # 獲取 POST 發來的數據,並更新到數據庫中 if request.method == "POST": # 獲取 POST 傳送來的 id 值和出版社 edit_id = request.POST.get(‘id‘) new_name = request.POST.get(‘publisher_name‘) # 根據 id 取得出版社 publisher = models.Publisher.objects.get(id=edit_id) publisher.name = new_name publisher.save() # 把修改的結果提交到數據庫 return redirect("/publisher_list/") # 跳轉到列表頁面 # 從 GET 請求中取得 id 值 publisher_id = request.GET.get(‘id‘) if publisher_id: # 獲取當前編輯的出版社對象 publisher_obj = models.Publisher.objects.get(id=publisher_id) return render(request, "edit_publisher.html", {"publisher": publisher_obj}) else: return HttpResponse("編輯的出版社不存在!") # 展示書籍列表 def book_list(request): # 去數據庫中查詢所有書籍 all_book = models.Book.objects.all() # 渲染數據 return render(request, "book_list.html", {"book_list": all_book})
在 mysite0/urls.py 中添加對應關系
from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r‘^publisher_list/‘, views.publisher_list), url(r‘^add_publisher/‘, views.add_publisher), url(r‘^del_publisher/‘, views.del_publisher), url(r‘^edit_publisher/‘, views.edit_publisher), url(r‘^book_list/‘, views.book_list), ]
運行結果:
添加書籍:
修改 book_list.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>書籍列表</title> </head> <body> <table border="1"> <thead> <tr> <th>id</th> <th>書名</th> <th>出版社</th> </tr> </thead> <tbody> {% for book in book_list %} <tr> <td>{{ book.id }}</td> <td>{{ book.title }}</td> <td>{{ book.publisher.name }}</td> </tr> {% endfor %} </tbody> </table> <a href="/add_book/">添加書籍</a> </body> </html>
創建 add_book.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>添加書籍</title> </head> <body> <h1>添加書籍</h1> <form action="/add_book/" method="post"> <p> 書名:<input type="text" name="book_title"> </p> <p> 出版社: <select name="publisher" > {% for publisher in publisher_list %} <option value="{{ publisher.id }}">{{ publisher.name }}</option> {% endfor %} </select> </p> <p> <input type="submit" value="提交"> </p> </form> </body> </html>
在 app01/views.py 中添加 add_book 函數
from django.shortcuts import render, redirect, HttpResponse from app01 import models # 展示出版社列表 def publisher_list(request): # 去數據庫查出所有的出版社,填充到 html 中,返回給用戶 ret = models.Publisher.objects.all().order_by("id") # order_by("id") 通過 id 進行排序 return render(request, "publisher_list.html", {"publisher_list": ret}) # 添加新的出版社 def add_publisher(request): # 如果是 POST 請求,就獲取用戶填寫的數據 if request.method == "POST": new_publisher = request.POST.get("publisher_name") # 獲得數據後去數據庫中新增一條數據 models.Publisher.objects.create(name=new_publisher) # 添加成功後進行跳轉 return redirect("/publisher_list/") # 用戶來到該界面返回的 html 頁面 return render(request, "add_publisher.html") # 刪除出版社 def del_publisher(request): # 從 GET 請求的參數中拿到要刪除的 id 值 del_id = request.GET.get(‘id‘) # 如果取到 id 值,就去數據庫中刪除該 id 的數據 if del_id: # 根據 id 查找數據,並刪除 del_obj = models.Publisher.objects.get(id=del_id).delete() # 刪除後返回頁面 return redirect("/publisher_list/") else: return HttpResponse("要刪除的數據不存在!") # 編輯出版社 def edit_publisher(request): # 獲取 POST 發來的數據,並更新到數據庫中 if request.method == "POST": # 獲取 POST 傳送來的 id 值和出版社 edit_id = request.POST.get(‘id‘) new_name = request.POST.get(‘publisher_name‘) # 根據 id 取得出版社 publisher = models.Publisher.objects.get(id=edit_id) publisher.name = new_name publisher.save() # 把修改的結果提交到數據庫 return redirect("/publisher_list/") # 跳轉到列表頁面 # 從 GET 請求中取得 id 值 publisher_id = request.GET.get(‘id‘) if publisher_id: # 獲取當前編輯的出版社對象 publisher_obj = models.Publisher.objects.get(id=publisher_id) return render(request, "edit_publisher.html", {"publisher": publisher_obj}) else: return HttpResponse("編輯的出版社不存在!") # 展示書籍列表 def book_list(request): # 去數據庫中查詢所有書籍 all_book = models.Book.objects.all() print(all_book) # 渲染數據 return render(request, "book_list.html", {"book_list": all_book}) # 添加書籍 def add_book(request): if request.method == "POST": new_title = request.POST.get("book_title") new_publisher_id = request.POST.get("publisher") # 去數據庫中創建數據 models.Book.objects.create(title=new_title, publisher_id=new_publisher_id) return redirect("/book_list/") # 去數據庫取得出版社數據展示在頁面上以供用戶選擇 publishers = models.Publisher.objects.all() return render(request, "add_book.html", {"publisher_list": publishers})
在 mysite0/urls.py 中添加對應關系
from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r‘^admin/‘, admin.site.urls), url(r‘^publisher_list/‘, views.publisher_list), url(r‘^add_publisher/‘, views.add_publisher), url(r‘^del_publisher/‘, views.del_publisher), url(r‘^edit_publisher/‘, views.edit_publisher), url(r‘^book_list/‘, views.book_list), url(r‘^add_book/‘, views.add_book), ]
運行結果:
點擊“添加書籍”
點擊“提交”
刪除書籍:
修改 book_list.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>書籍列表</title> </head> <body> <table border="1"> <thead> <tr> <th>id</th> <th>書名</th> <th>出版社</th> <th>操作</th> </tr> </thead> <tbody> {% for book in book_list %} <tr> <td>{{ book.id }}</td> <td>{{ book.title }}</td> <td>{{ book.publisher.name }}</td> <td> <a href="/del_book/?id={{ book.id }}">刪除</a> </td> </tr> {% endfor %} </tbody> </table> <a href="/add_book/">添加書籍</a> </body> </html>
在 app01/views.py 中添加 del_book 函數
from django.shortcuts import render, redirect, HttpResponse from app01 import models # 展示出版社列表 def publisher_list(request): # 去數據庫查出所有的出版社,填充到 html 中,返回給用戶 ret = models.Publisher.objects.all().order_by("id") # order_by("id") 通過 id 進行排序 return render(request, "publisher_list.html", {"publisher_list": ret}) # 添加新的出版社 def add_publisher(request): # 如果是 POST 請求,就獲取用戶填寫的數據 if request.method == "POST": new_publisher = request.POST.get("publisher_name") # 獲得數據後去數據庫中新增一條數據 models.Publisher.objects.create(name=new_publisher) # 添加成功後進行跳轉 return redirect("/publisher_list/") # 用戶來到該界面返回的 html 頁面 return render(request, "add_publisher.html") # 刪除出版社 def del_publisher(request): # 從 GET 請求的參數中拿到要刪除的 id 值 del_id = request.GET.get(‘id‘) # 如果取到 id 值,就去數據庫中刪除該 id 的數據 if del_id: # 根據 id 查找數據,並刪除 del_obj = models.Publisher.objects.get(id=del_id).delete() # 刪除後返回頁面 return redirect("/publisher_list/") else: return HttpResponse("要刪除的數據不存在!") # 編輯出版社 def edit_publisher(request): # 獲取 POST 發來的數據,並更新到數據庫中 if request.method == "POST": # 獲取 POST 傳送來的 id 值和出版社 edit_id = request.POST.get(‘id‘) new_name = request.POST.get(‘publisher_name‘) # 根據 id 取得出版社 publisher = models.Publisher.objects.get(id=edit_id) publisher.name = new_name publisher.save() # 把修改的結果提交到數據庫 return redirect("/publisher_list/") # 跳轉到列表頁面 # 從 GET 請求中取得 id 值 publisher_id = request.GET.get(‘id‘) if publisher_id: # 獲取當前編輯的出版社對象 publisher_obj = models.Publisher.objects.get(id=publisher_id) return render(request, "edit_publisher.html", {"publisher": publisher_obj}) else: return HttpResponse("編輯的出版社不存在!") # 展示書籍列表 def book_list(request): # 去數據庫中查詢所有書籍 all_book = models.Book.objects.all() print(all_book) # 渲染數據 return render(request, "book_list.html", {"book_list": all_book}) # 添加書籍 def add_book(request): if request.method == "POST": new_title = request.POST.get("book_title") new_publisher_id = request.POST.get("publisher") # 去數據庫中創建數據 models.Book.objects.create(title=new_title, publisher_id=new_publisher_id) return redirect("/book_list/") # 去數據庫取得出版社數據展示在頁面上以供用戶選擇 publishers = models.Publisher.objects.all() return render(request, "add_book.html", {"publisher_list": publishers}) # 刪除書籍 def del_book(request): # 從 URL 中獲取要刪除的書籍的 id del_id = request.GET.get("id") # 去數據庫中刪除指定 id 的書籍 models.Book.objects.get(id=del_id).delete() # 跳轉到書籍列表頁面 return redirect("/book_list/")
在 mysite0/urls.py 中添加對應關系
from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r‘^admin/‘, admin.site.urls), url(r‘^publisher_list/‘, views.publisher_list), url(r‘^add_publisher/‘, views.add_publisher), url(r‘^del_publisher/‘, views.del_publisher), url(r‘^edit_publisher/‘, views.edit_publisher), url(r‘^book_list/‘, views.book_list), url(r‘^add_book/‘, views.add_book), url(r‘^del_book/‘, views.del_book), ]
運行結果:
刪除“PHP”
頁面閃了一下,PHP 就被刪除了
Python - Django - ORM 實例(二)