在 app01/models.py 中添加 Book 類對象表html
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")
而後執行命令更新到數據庫中python
manage.py@mysite0 > makemigrations manage.py@mysite0 > migrate
在 Book 表中添加 3 條數據數據庫
建立 book_list.html:django
<!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 對象app
在 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 中添加對應關係post
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), ]
運行結果:url
修改 book_list.html:3d
<!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:orm
<!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 就被刪除了
編輯書籍:
修改 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> <a href="/edit_book/?id={{ book.id }}">編輯</a> </td> </tr> {% endfor %} </tbody> </table> <a href="/add_book/">添加書籍</a> </body> </html>
建立 edit_book.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>編輯書籍</title> </head> <body> <h1>編輯書籍</h1> <form action="/edit_book/" method="post"> <input type="text" style="display: none" name="id" value="{{ book_obj.id }}"> <p> 書名: <input type="text" name="book_title" value="{{ book_obj.title }}"> </p> <p> 出版社: <select name="publisher"> {% for publisher in publisher_list %} {# 經過 if 條件判斷來選擇默認出版社 #} {% if book_obj.publisher_id == publisher.id %} {# 默認選擇當前書籍關聯的出版社 #} <option selected value="{{ publisher.id }}">{{ publisher.name }}</option> {% else %} {# 其餘的出版社不選中 #} <option value="{{ publisher.id }}">{{ publisher.name }}</option> {% endif %} {% endfor %} </select> </p> <p> <input type="submit" value="提交"> </p> </form> </body> </html>
在 app01/views.py 中添加 edit_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/") # 編輯書籍 def edit_book(request): # 從 POST 數據中提取書籍 id 和書籍名及出版社 if request.method == "POST": edit_id = request.POST.get("id") new_title = request.POST.get("book_title") new_publisher_id = request.POST.get("publisher") # 更新數據 edit_book_obj = models.Book.objects.get(id=edit_id) edit_book_obj.title = new_title edit_book_obj.publisher_id = new_publisher_id # 將更新的數據提交到數據庫中 edit_book_obj.save() # 跳轉到書籍列表頁面 return redirect("/book_list/") # 取得編輯書籍的 id book_id = request.GET.get("id") # 根據 id 去數據庫中取得書籍數據 book_obj = models.Book.objects.get(id=book_id) publisher_obj = models.Publisher.objects.all() return render(request, "edit_book.html", {"publisher_list": publisher_obj, "book_obj": book_obj})
在 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), url(r'edit_book/', views.edit_book), ]
運行結果:
編輯「《C++》」
改爲「《C》」,「乙出版社」