日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達者為師

網(wǎng)站首頁 編程語言 正文

Python實現(xiàn)簡易的圖書管理系統(tǒng)_python

作者:長大的小螞蟻 ? 更新時間: 2022-05-13 編程語言

本文實例為大家分享了Python實現(xiàn)簡易圖書管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下

首先展示一下圖書管理系統(tǒng)的首頁:

這是圖書管理系統(tǒng)的發(fā)布圖書頁面:

最后是圖書管理系統(tǒng)的圖書詳情頁已經(jīng)圖書進行刪除的管理頁。

該圖書管理系統(tǒng)為練習(xí)階段所做,能夠?qū)崿F(xiàn)圖書詳情的查詢、圖書的添加、圖書的刪除功能。以下附源碼:

views.py文件中代碼如下:

from django.shortcuts import render,redirect,reverse
from django.db import connection


# 因為在以下幾個視圖函數(shù)中都會用到cursor對象,所以在這里就定義為一個函數(shù)。
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, '水滸傳', '施耐庵'))

? ? # 此時我們?nèi)绻胍跒g覽器中進行顯示的話,就要傳遞給DTL模板.
? ? # 可以直接不用定義中間變量context,直接將獲取到的元組,傳遞給render()函數(shù)中的參數(shù)context,
? ? # 就比如,這種情況就是定義了一個中間變量。
? ? # context = {
? ? # ? ? 'books':books
? ? # }

? ? # 以下我們直接將獲取到的元組傳遞給render()函數(shù)。
? ? return render(request,'index.html',context={'books':books})


def add_book(request):
? ? # 因為DTL模板中是采用post請求進行提交數(shù)據(jù)的,因此需要首先判斷數(shù)據(jù)是以哪種方式提交過來的
? ? # 如果是以get請求的方式提交的,就直接返回到發(fā)布圖書的視圖,否則的話,就將數(shù)據(jù)添加到數(shù)據(jù)庫表中
? ? # 一定要注意:此處的GET一定是提交方式GET.不是一個函數(shù)get()
? ? # 否則的話,會造成,每次點擊“發(fā)布圖書”就會添加一條數(shù)據(jù)信息全為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時,因為二者在數(shù)據(jù)庫中的類型均為字符串的類型,
? ? ? ? # 所以在這里,就需要使用單引號進行包裹,執(zhí)行以下語句,就可以將數(shù)據(jù)插入到數(shù)據(jù)庫中了。
? ? ? ? cursor.execute("insert into db01(id,name,author) values(null ,'%s','%s')" % (name,author))
? ? ? ? # 返回首頁,可以使用重定向的方式,并且可以將視圖函數(shù)的url名進行反轉(zhuǎn)。
? ? ? ? return redirect(reverse('index'))


def book_detail(request,book_id):
? ? ? ? cursor = get_cursor()
? ? ? ? # 將提交上來的數(shù)據(jù)與數(shù)據(jù)庫中的id進行對比,如果相符合,就會返回一個元組
? ? ? ? # (根據(jù)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):
? ? # 判斷提交數(shù)據(jù)的方式是否是post請求
? ? if request.method == "POST":
? ? ? ? # 使用POST方式獲取圖書的book_id,
? ? ? ? book_id = request.POST.get('book_id')
? ? ? ? # 將獲取的圖書的book_id與數(shù)據(jù)庫中的id信息進行比較
? ? ? ? cursor = get_cursor()
? ? ? ? # 注意:此處刪除的信息不能寫明屬性,否者的話,會提交數(shù)據(jù)不成功。
? ? ? ? # 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("提交數(shù)據(jù)的方式不是post請求")

因為各個頁面的header部分都是相同的。所以在這里采用模板繼承的方式,進行實現(xiàn)模板結(jié)構(gòu)的優(yōu)化。

其中父模板base.html代碼如下:




? ? 
? ? 圖書管理系統(tǒng)
? ? 


? ?
{% block content %} {% endblock %}

首頁index.html模板中代碼如下:

{% extends 'base.html' %}


{% block content %}
? ? 
? ? ? ? 
? ? ? ? 
? ? ? ? ? ? 
? ? ? ? ? ? 
? ? ? ? ? ? 
? ? ? ? 
? ? ? ? 
? ? ? ? 
? ? ? ? {% for book in books %}
? ? ? ? ? ? 
? ? ? ? ? ? ? ? 
? ? ? ? ? ? ? ? 
? ? ? ? ? ? ? ? 
? ? ? ? ? ? 
? ? ? ? {% endfor %}

? ? ? ? 
? ? 
序號書名作者
{{ forloop.counter }}{{ book.1 }}{{ book.2 }}
{% endblock %}

發(fā)布圖書模板add_book.html中的代碼如下:

{% extends 'base.html' %}


{% block content %}
{# ?其中,這個action表示的當前的這個表單內(nèi)容,你要提交給那個視圖進行接收,#}
{# ? ?在這里我們提交給當前視圖add_book()進行處理數(shù)據(jù),就不用寫了 ?#}
{# ?而method方法是采用哪種方式進行提交表單數(shù)據(jù),常用的有g(shù)et,post,在這里采用post請求。 ?#}
? ? 
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
書名:
作者:
? ?
{% endblock %}

在urls.py中視圖函數(shù)與url進行適當?shù)挠成洌?/p>

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//', views.book_detail, name = 'detail'),
? ? path('book/del/',views.del_book, name = 'del'),
]

并且需要注意的是:在settings.py文件中需要關(guān)閉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文件中,添加加載靜態(tài)文件的路徑變量

STATICFILES_DIRS = [
? ? os.path.join(BASE_DIR,'static')
]

并且設(shè)置pycharm與mysql數(shù)據(jù)庫連接的信息:

DATABASES = {
? ? 'default': {
? ? ? ? 'ENGINE': 'django.db.backends.mysql',
? ? ? ? 'NAME': 'db01',
? ? ? ? 'USERNAME': 'root',
? ? ? ? 'PASSWORD': 'root',
? ? ? ? 'HOST': '127.0.0.1',
? ? ? ? 'PORT': '3306'
? ? }
}

原文鏈接:https://blog.csdn.net/zjy123078_zjy/article/details/104035218

欄目分類
最近更新