Python實現(xiàn)簡易的圖書管理系統(tǒng)
本文實例為大家分享了Python實現(xiàn)簡易圖書管理系統(tǒng)的具體代碼,供大家參考,具體內容如下
首先展示一下圖書管理系統(tǒng)的首頁:
這是圖書管理系統(tǒng)的發(fā)布圖書頁面:
最后是圖書管理系統(tǒng)的圖書詳情頁已經(jīng)圖書進行刪除的管理頁。
該圖書管理系統(tǒng)為練習階段所做,能夠實現(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, '水滸傳', '施耐庵')) ? ? # 此時我們如果想要在瀏覽器中進行顯示的話,就要傳遞給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名進行反轉。 ? ? ? ? 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)模板結構的優(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>序號</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 %} {# ?其中,這個action表示的當前的這個表單內容,你要提交給那個視圖進行接收,#} {# ? ?在這里我們提交給當前視圖add_book()進行處理數(shù)據(jù),就不用寫了 ?#} {# ?而method方法是采用哪種方式進行提交表單數(shù)據(jù),常用的有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中視圖函數(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/<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文件中,添加加載靜態(tài)文件的路徑變量
STATICFILES_DIRS = [ ? ? os.path.join(BASE_DIR,'static') ]
并且設置pycharm與mysql數(shù)據(jù)庫連接的信息:
DATABASES = { ? ? 'default': { ? ? ? ? 'ENGINE': 'django.db.backends.mysql', ? ? ? ? 'NAME': 'db01', ? ? ? ? 'USERNAME': 'root', ? ? ? ? 'PASSWORD': 'root', ? ? ? ? 'HOST': '127.0.0.1', ? ? ? ? 'PORT': '3306' ? ? } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
深入理解Python虛擬機中調試器實現(xiàn)原理與源碼分析
本文主要給大家介紹python中調試器的實現(xiàn)原理,通過了解一個語言的調試器的實現(xiàn)原理我們可以更加深入的理解整個語言的運行機制,可以幫助我們更好的理解程序的執(zhí)行,感興趣的可以了解一下2023-04-04