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

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

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

該圖書管理系統(tǒng)為練習(xí)階段所做,能夠?qū)崿F(xiàn)圖書詳情的查詢、圖書的添加、圖書的刪除功能。以下附源碼:
views.py文件中代碼如下:
from django.shortcuts import render,redirect,reverse
from django.db import connection
# 因?yàn)樵谝韵聨讉€(gè)視圖函數(shù)中都會(huì)用到cursor對(duì)象,所以在這里就定義為一個(gè)函數(shù)。
def get_cursor():
? ? return connection.cursor()
def index(request):
? ? cursor = get_cursor()
? ? cursor.execute("select * from db01")
? ? books = cursor.fetchall()
? ? # 此時(shí)的books就是一個(gè)包含多個(gè)元組的元組
? ? # 打印進(jìn)行查看
? ? # print(books)
? ? # ((1, '三國演義', '羅貫中'), (2, '西游記', '羅貫中'), (3, '水滸傳', '施耐庵'))
? ? # 此時(shí)我們?nèi)绻胍跒g覽器中進(jìn)行顯示的話,就要傳遞給DTL模板.
? ? # 可以直接不用定義中間變量context,直接將獲取到的元組,傳遞給render()函數(shù)中的參數(shù)context,
? ? # 就比如,這種情況就是定義了一個(gè)中間變量。
? ? # context = {
? ? # ? ? 'books':books
? ? # }
? ? # 以下我們直接將獲取到的元組傳遞給render()函數(shù)。
? ? return render(request,'index.html',context={'books':books})
def add_book(request):
? ? # 因?yàn)镈TL模板中是采用post請(qǐng)求進(jìn)行提交數(shù)據(jù)的,因此需要首先判斷數(shù)據(jù)是以哪種方式提交過來的
? ? # 如果是以get請(qǐng)求的方式提交的,就直接返回到發(fā)布圖書的視圖,否則的話,就將數(shù)據(jù)添加到數(shù)據(jù)庫表中
? ? # 一定要注意:此處的GET一定是提交方式GET.不是一個(gè)函數(shù)get()
? ? # 否則的話,會(huì)造成,每次點(diǎn)擊“發(fā)布圖書”就會(huì)添加一條數(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()
? ? ? ? # 在進(jìn)行獲取name,author時(shí),因?yàn)槎咴跀?shù)據(jù)庫中的類型均為字符串的類型,
? ? ? ? # 所以在這里,就需要使用單引號(hào)進(jìn)行包裹,執(zhí)行以下語句,就可以將數(shù)據(jù)插入到數(shù)據(jù)庫中了。
? ? ? ? cursor.execute("insert into db01(id,name,author) values(null ,'%s','%s')" % (name,author))
? ? ? ? # 返回首頁,可以使用重定向的方式,并且可以將視圖函數(shù)的url名進(jìn)行反轉(zhuǎn)。
? ? ? ? return redirect(reverse('index'))
def book_detail(request,book_id):
? ? ? ? cursor = get_cursor()
? ? ? ? # 將提交上來的數(shù)據(jù)與數(shù)據(jù)庫中的id進(jìn)行對(duì)比,如果相符合,就會(huì)返回一個(gè)元組
? ? ? ? # (根據(jù)id的唯一性,只會(huì)返回一個(gè)元組)
? ? ? ? # 在這里只獲取了name,auhtor字段的信息
? ? ? ? cursor.execute("select id,name,author from db01 where id=%s" % book_id)
? ? ? ? # 可以使用fetchone()進(jìn)行獲取。
? ? ? ? book = cursor.fetchone()
? ? ? ? # 之后拿到這個(gè)圖書的元組之后,就可以將它渲染到模板中了。
? ? ? ? return render(request,'book_detail.html',context={'book':book})
def del_book(request):
? ? # 判斷提交數(shù)據(jù)的方式是否是post請(qǐng)求
? ? if request.method == "POST":
? ? ? ? # 使用POST方式獲取圖書的book_id,
? ? ? ? book_id = request.POST.get('book_id')
? ? ? ? # 將獲取的圖書的book_id與數(shù)據(jù)庫中的id信息進(jìn)行比較
? ? ? ? cursor = get_cursor()
? ? ? ? # 注意:此處刪除的信息不能寫明屬性,否者的話,會(huì)提交數(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請(qǐng)求")因?yàn)楦鱾€(gè)頁面的header部分都是相同的。所以在這里采用模板繼承的方式,進(jìn)行實(shí)現(xiàn)模板結(jié)構(gòu)的優(yōu)化。
其中父模板base.html代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
? ? <meta charset="UTF-8">
? ? <title>圖書管理系統(tǒng)</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' %}">發(fā)布圖書</a></li>
? ? ? ? </ul>
? ? </nav>
</header>
{% block content %}
{% endblock %}
<footer>
</footer>
</body>
</html>首頁index.html模板中代碼如下:
{% extends 'base.html' %}
{% block content %}
? ? <table>
? ? ? ? <thead>
? ? ? ? <tr>
? ? ? ? ? ? <th>序號(hào)</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 %}發(fā)布圖書模板add_book.html中的代碼如下:
{% extends 'base.html' %}
{% block content %}
{# ?其中,這個(gè)action表示的當(dāng)前的這個(gè)表單內(nèi)容,你要提交給那個(gè)視圖進(jìn)行接收,#}
{# ? ?在這里我們提交給當(dāng)前視圖add_book()進(jìn)行處理數(shù)據(jù),就不用寫了 ?#}
{# ?而method方法是采用哪種方式進(jìn)行提交表單數(shù)據(jù),常用的有g(shù)et,post,在這里采用post請(qǐng)求。 ?#}
? ? <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中視圖函數(shù)與url進(jìn)行適當(dāng)?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/<int:book_id>/', views.book_detail, name = 'detail'),
? ? path('book/del/',views.del_book, name = 'del'),
]并且需要注意的是:在settings.py文件中需要關(guān)閉csrf的驗(yàn)證。
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'
? ? }
}以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python模擬實(shí)現(xiàn)圖書管理系統(tǒng)
- python實(shí)現(xiàn)簡易圖書管理系統(tǒng)
- python編寫圖書管理系統(tǒng)
- Python實(shí)現(xiàn)圖書借閱管理系統(tǒng)
- Python實(shí)現(xiàn)圖書管理系統(tǒng)設(shè)計(jì)
- python面向?qū)ο蠓▽?shí)現(xiàn)圖書管理系統(tǒng)
- wxpython實(shí)現(xiàn)圖書管理系統(tǒng)
- python圖書管理系統(tǒng)
- python實(shí)現(xiàn)圖書管理系統(tǒng)
- Python實(shí)現(xiàn)簡單的圖書管理系統(tǒng)
相關(guān)文章
Python中不同類之間調(diào)用方法的四種方式小結(jié)
類是一種面向?qū)ο蟮木幊谭妒?它允許我們將數(shù)據(jù)和功能封裝在一個(gè)實(shí)體中,本文主要介紹了Python中不同類之間調(diào)用方法的四種方式小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02
tensorflow2.10使用BERT實(shí)現(xiàn)Semantic Similarity過程解析
這篇文章主要為大家介紹了tensorflow2.10使用BERT實(shí)現(xiàn)Semantic Similarity過程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
Python開發(fā)如何在ubuntu 15.10 上配置vim
這篇文章主要介紹了Python開發(fā)如何在ubuntu 15.10 上配置vim 的相關(guān)資料,需要的朋友可以參考下2016-01-01
python使用pip成功導(dǎo)入庫后還是報(bào)錯(cuò)的解決方法(針對(duì)vscode)
最近在學(xué)爬蟲,但在使用Scrapy包時(shí),在終端通過pip裝好包以后,在pycharm中導(dǎo)入包時(shí),依然會(huì)報(bào)錯(cuò),下面這篇文章主要給大家介紹了關(guān)于python使用pip成功導(dǎo)入庫后還是報(bào)錯(cuò)的解決方法,需要的朋友可以參考下2022-07-07
淺析python3字符串格式化format()函數(shù)的簡單用法
這篇文章主要介紹了python3字符串格式化format()函數(shù)的簡單用法,代碼簡單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-12-12
Python中的response.text與content區(qū)別詳解
這篇文章主要介紹了Python中的response.text與content區(qū)別詳解,?從網(wǎng)絡(luò)請(qǐng)求下來的數(shù)據(jù),他們都是字節(jié)類型的,如果服務(wù)器不指定的話,默認(rèn)編碼是"ISO-8859-1",我們使用text直接拿到的是字符串類型,沒有進(jìn)行解碼操作,則會(huì)出現(xiàn)亂碼問題,需要的朋友可以參考下2023-12-12
Python如何import文件夾下的文件(實(shí)現(xiàn)方法)
下面小編就為大家?guī)硪黄狿ython如何import文件夾下的文件(實(shí)現(xiàn)方法)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01

