我們可以先定義數據表的結構
創建一個出版社的id和name(models.py文件)
# 出版社 class Publisher(models.Model): # 自增的主鍵id id = models.AutoField(primary_key=True) # 創建一個varchar(64)的唯一的不爲空的字段 name = models.CharField(max_length=64, null=False, unique=True)
將改動記錄下來
python manage.py makemigrations
去數據庫執行
python manage.py migrate
然後可以通過pycharm中的database工具進行數據庫的編輯
下一步就是建立url對應關係(urls.py)
from django.conf.urls import url from django.contrib import admin from myapp import views urlpatterns = [ url(r'^admin/', admin.site.urls), # 出版社列表 url(r'^publisher_list/', views.publisher_list), ]
去views.py文件中建立publisher_list函數
函數內部要完成的功能是
- 去數據庫查詢出所有出版社,
- 填充到html中,
- 返回給用戶
from django.shortcuts import render, HttpResponse from myapp import models def publisher_list(request): ret = models.Publisher.objects.all()
# 輸出一個列表對象[<Publisher: Publisher object>, <Publisher: Publisher object>, <Publisher: Publisher object>] # print(ret[1].id, ret[1].name) #id和名字拿出來了 #render渲染頁面 #在publisher_list.html頁面通過publisher_list變量就可以獲取到ret了 return render(request, "publisher_list.html", {"publisher_list": ret})
在templates目錄下新建publisher_list.html文件來進行展示給用戶
<table border="1"> <thead> <tr> <th>id值</th> <th>name</th> </tr> </thead> <tbody> {% for publisher in publisher_list %} <tr> <td>{{ publisher.id }}</td> <td>{{ publisher.name }}</td> </tr> {% endfor %} </tbody> </table>
- 特殊的模板循環語言
這樣運行django項目,訪問對應的url就可以看到將數據表的信息展示出來了
在瀏覽器頁面的內容如下(http://127.0.0.1:8000/publisher_list/):
然後我們可以在頁面添加功能,比如:添加出版社, 刪除出版社, 編輯出版社
添加出版社
1,在urls.py文件中添加對應URl對應關係
# 添加出版社 url(r'^add_publisher/', views.add_publisher),
2,在views.py中創建對應的邏輯函數
# 添加出版社 def add_publisher(request): if request.method == "POST": # 用戶填寫了新的用戶名,併發送了post請求過來 new_publisher = request.POST.get('new_publisher') if new_publisher: # 去數據庫中創建一條數據庫記錄 models.Publisher.objects.create(name=new_publisher) else: return HttpResponse("出版社名字不能爲空") # 添加成功之後將publisher_list頁面返回給用戶 return redirect('/publisher_list/') # 點擊添加用戶時,返回一個頁面,有個框,用於添加數據 return render(request, "add_publisher.html")
3在templates目錄下建立add_publisher.html用於添加出版社頁面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>出版社列表</title> </head> <body> <a href="/add_publisher/">添加出版社</a> <table border="1"> <thead> <tr> <th>id值</th> <th>name</th> <th>操作</th> </tr> </thead> <tbody> {% for publisher in publisher_list %} <tr> <td>{{ publisher.id }}</td> <td>{{ publisher.name }}</td> <td> <a href="/delete_publisher/?id={{ publisher.id }}">刪除</a> <a href="/edit_publisher/?id={{ publisher.id }}">編輯</a> </td> </tr> {% endfor %} </tbody> </table> </body> </html>
這樣就完成了添加出版社的功能了
接下來就是刪除出版社的功能(根據id來刪除)
urls.py中
# 刪除出版社 url(r'^delete_publisher', views.delete_publisher),
views.py中
# 刪除出版社 def delete_publisher(request): # 刪除制定的數據 # 1從GET請求的參數裏面拿到將要刪除的id值 delete_id = request.GET.get('id',None) # 如果能夠獲取到數據 if delete_id: # 根據id值查找到數據 delete_obj = models.Publisher.objects.get(id=delete_id) # 刪除 delete_obj.delete() # 返回刪除後的頁面,跳轉到出版社的列表頁,查看是否刪除成功 return redirect("/publisher_list/") else: return HttpResponse("要刪除的數據不存在")
現在就可以刪除數據了,點擊刪除就會在數據庫中刪除數據
接下來就是編輯功能了(用來實現更改出版社的name)
urls.py文件
# 編輯出版社 url(r'^edit_publisher', views.edit_publisher),
views.py文件
# 編輯出版社 def edit_publisher(request): # 用戶修改完出版社的名稱,點擊提交,在這裏獲取到 if request.method == "POST": # print(request.POST) #檢查post能夠獲取那些信息 #取新出版社的名字 edit_id = request.POST.get("_id") new_name = request.POST.get("_name") # 根據id,來編輯出版社 edit_publisher = models.Publisher.objects.get(id=edit_id) edit_publisher.name = new_name edit_publisher.save() #更新到數據庫 # 跳轉到出版社列表頁,查看是否修改成功 return redirect("/publisher_list/") # 從GET請求的url中取到id屬性 edit_id = request.GET.get("id") if edit_id: # 獲取到當前編輯的出版社 publisher_obj = models.Publisher.objects.get(id=edit_id) return render(request, "edit_publisher.html", {"publisher":publisher_obj})
edit_publisher.html文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>編輯出版社</title> </head> <body> <h1>編輯出版社</h1> <form action="/edit_publisher/" method="post"> <p> <input type="text" name="_id" value="{{ publisher.id }}" style="display: none"> </p> <p> <input type="text" name="_name" value="{{ publisher.name }}"> </p> <p> <input type="submit" value="更新到數據庫"> </p> </form> </body> </html>
最後的效果