首先展示一下圖書管理系統的首頁:

這是圖書管理系統的發布圖書頁面:

最后是圖書管理系統的圖書詳情頁已經圖書進行刪除的管理頁。

該圖書管理系統為練習階段所做,能夠實現圖書詳情的查詢、圖書的添加、圖書的刪除功能。以下附源碼:
views.py文件中代碼如下:
from django.shortcuts import render,redirect,reverse
from django.db import connection
# 因為在以下幾個視圖函數中都會用到cursor對象,所以在這里就定義為一個函數。
def get_cursor():
return connection.cursor()
def index(request):
cursor = get_cursor()
cursor.execute("select * from db01")
books = cursor.fetchall()
# 此時的books就是一個包含多個元組的元組
# 打印進行查看
# print(books)
# ((1, '三國演義', '羅貫中'), (2, '西游記', '羅貫中'), (3, '水滸傳', '施耐庵'))
# 此時我們如果想要在瀏覽器中進行顯示的話,就要傳遞給DTL模板.
# 可以直接不用定義中間變量context,直接將獲取到的元組,傳遞給render()函數中的參數context,
# 就比如,這種情況就是定義了一個中間變量。
# context = {
# 'books':books
# }
# 以下我們直接將獲取到的元組傳遞給render()函數。
return render(request,'index.html',context={'books':books})
def add_book(request):
# 因為DTL模板中是采用post請求進行提交數據的,因此需要首先判斷數據是以哪種方式提交過來的
# 如果是以get請求的方式提交的,就直接返回到發布圖書的視圖,否則的話,就將數據添加到數據庫表中
# 一定要注意:此處的GET一定是提交方式GET.不是一個函數get()
# 否則的話,會造成,每次點擊“發布圖書”就會添加一條數據信息全為None,None
if request.method == "GET":
return render(request,'add_book.html')
else:
name = request.POST.get('name')
author = request.POST.get('author')
cursor = get_cursor()
# 在進行獲取name,author時,因為二者在數據庫中的類型均為字符串的類型,
# 所以在這里,就需要使用單引號進行包裹,執行以下語句,就可以將數據插入到數據庫中了。
cursor.execute("insert into db01(id,name,author) values(null ,'%s','%s')" % (name,author))
# 返回首頁,可以使用重定向的方式,並且可以將視圖函數的url名進行反轉。
return redirect(reverse('index'))
def book_detail(request,book_id):
cursor = get_cursor()
# 將提交上來的數據與數據庫中的id進行對比,如果相符合,就會返回一個元組
# (根據id的唯一性,只會返回一個元組)
# 在這里只獲取了name,auhtor字段的信息
cursor.execute("select id,name,author from db01 where id=%s" % book_id)
# 可以使用fetchone()進行獲取。
book = cursor.fetchone()
# 之后拿到這個圖書的元組之后,就可以將它渲染到模板中了。
return render(request,'book_detail.html',context={'book':book})
def del_book(request):
# 判斷提交數據的方式是否是post請求
if request.method == "POST":
# 使用POST方式獲取圖書的book_id,
book_id = request.POST.get('book_id')
# 將獲取的圖書的book_id與數據庫中的id信息進行比較
cursor = get_cursor()
# 注意:此處刪除的信息不能寫明屬性,否者的話,會提交數據不成功。
# cursor.execute("delete id,name,author from db01 where id = '%s'" % book_id)
cursor.execute("delete from db01 where id=%s" % book_id)
return redirect(reverse('index'))
else:
raise RuntimeError("提交數據的方式不是post請求")
其中父模板base.html代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>圖書管理系統</title>
<link rel="stylesheet" href="{% static 'index.css' %}">
</head>
<body>
<header>
<nav>
<ul class="nav">
<li><a href="/">首頁</a></li>
<li><a href="{% url 'add' %}">發布圖書</a></li>
</ul>
</nav>
</header>
{% block content %}
{% endblock %}
<footer>
</footer>
</body>
</html>
首頁index.html模板中代碼如下:
{% extends 'base.html' %}
{% block content %}
<table>
<thead>
<tr>
<th>序號</th>
<th>書名</th>
<th>作者</th>
</tr>
</thead>
<tbody>
{% for book in books %}
<tr>
<td>{{ forloop.counter }}</td>
<td><a href="{% url 'detail' book_id=book.0 %}">{{ book.1 }}</a></td>
<td>{{ book.2 }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
發布圖書模板add_book.html中的代碼如下:
{% extends 'base.html' %}
{% block content %}
{# 其中,這個action表示的當前的這個表單內容,你要提交給那個視圖進行接收,#}
{# 在這里我們提交給當前視圖add_book()進行處理數據,就不用寫了 #}
{# 而method方法是采用哪種方式進行提交表單數據,常用的有get,post,在這里采用post請求。 #}
<form action="" method="post">
<table>
<tr>
<td>書名:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>作者:</td>
<td><input type="text" name="author"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="提交" name="sub"></td>
</tr>
</table>
</form>
{% endblock %}
在urls.py中視圖函數與url進行適當的映射:
from django.urls import path
from front import views
urlpatterns = [
path('', views.index, name = 'index'),
path('add/', views.add_book, name = 'add'),
path('book/detail/<int:book_id>/', views.book_detail, name = 'detail'),
path('book/del/',views.del_book, name = 'del'),
]
並且需要注意的是:在settings.py文件中需要關閉csrf的驗證。
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
# 'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
並且在settings.py文件中,添加加載靜態文件的路徑變量
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'static')
]
並且設置pycharm與mysql數據庫連接的信息:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db01',
'USERNAME': 'root',
'PASSWORD': 'root',
'HOST': '127.0.0.1',
'PORT': '3306'
}
}