Django框架搭建的簡易圖書信息網(wǎng)站案例
本文實(shí)例講述了Django框架搭建的簡易圖書信息網(wǎng)站。分享給大家供大家參考,具體如下:
創(chuàng)建Django項(xiàng)目,將數(shù)據(jù)庫改為mysql,修改項(xiàng)目的urls.py文件
創(chuàng)建一個(gè)新應(yīng)用,在應(yīng)用里創(chuàng)建urls.py文件。
在應(yīng)用的models.py里建表
from django.db import models # Create your models here. #一類 class BookInfo(models.Model): btitle=models.CharField(max_length=20)#圖書名稱 bpub_date=models.DateField()#出版日期 bread=models.IntegerField(default=0)#閱讀量,默認(rèn)為0 bcomment=models.IntegerField(default=0)#評(píng)論量 isDlete=models.BooleanField(default=False)#邏輯刪除,默認(rèn)不刪除 #多類 class HeroInfo(models.Model): hname=models.CharField(max_length=20) hgender=models.BooleanField(default=False) hcomment=models.CharField(max_length=200) #定義一個(gè)關(guān)系屬性 hbook=models.ForeignKey('BookInfo') isDlete = models.BooleanField(default=False) # 邏輯刪除,默認(rèn)不刪除
首頁index.html查詢所有圖書信息,在views.py里完善index函數(shù)。
def index(request): # 1.查詢出所有圖書的信息 books = BookInfo.objects.all() # 2.使用模板 return render(request, 'booktest/index.html', {'books': books})
在template文件夾下的booketest文件夾下新建index.html文件。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>圖書信息</title> </head> <body> <a href="/create" rel="external nofollow" >新增</a> <ul> {% for book in books %} <li><a href="/books{{ book.id }}" rel="external nofollow" >{{ book.btitle }}</a><a href="/delete{{ book.id }}" rel="external nofollow" >_刪除</a></li> {% endfor %} </ul> </body> </html>
index.html里有一個(gè)create新增按鈕,去view.py里添加create處理函數(shù)
def create(request): '''新增一本圖書''' # 1.創(chuàng)建一個(gè)bookinfo對(duì)象 b = BookInfo() b.btitle = '流星蝴蝶劍' b.bpub_date = date(1990, 1, 1) # 2.保存進(jìn)數(shù)據(jù)庫 b.save() # 3.返回應(yīng)答 # return HttpResponse('ok') # 讓瀏覽器返回首頁 return HttpResponseRedirect('/index')
數(shù)據(jù)庫里添加上之后,重定向到首頁index。
應(yīng)用的urls.py文件里要寫url(r'^create$',views.create)
,才能正確的跳轉(zhuǎn)到create處理函數(shù)。
一個(gè)顯示書里人物的details.html,從index.html用book.id去尋找書的數(shù)據(jù)。
去views.py寫details處理函數(shù)
def details(request,bid): book=BookInfo.objects.get(id=bid) heros=book.heroinfo_set.all() return render(request,'booktest/details.html',{'book':book,'heros':heros})
新建details.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>{{book.btitle}}</h1> 英雄信息: <br/> <ul> {% for hero in heros %} <li>{{hero.hname}}--{{hero.hcomment}}</li> {% empty %} 沒有英雄信息 {% endfor %} </ul> </body> </html>
去應(yīng)用的urls.py里把url地址和視圖處理函數(shù)對(duì)應(yīng)上
url(r'^books(\d+)$',views.details)
這里的(\d+)是需要傳參到details視圖處理函數(shù)。
github:https://github.com/zhangyuespec/Django
希望本文所述對(duì)大家基于Django框架的Python程序設(shè)計(jì)有所幫助。
- 使用Django搭建網(wǎng)站實(shí)現(xiàn)商品分頁功能
- 基于django micro搭建網(wǎng)站實(shí)現(xiàn)加水印功能
- python3.6+django2.0+mysql搭建網(wǎng)站過程詳解
- Python+Django搭建自己的blog網(wǎng)站
- Python3創(chuàng)建Django項(xiàng)目的幾種方法(3種)
- Django 實(shí)現(xiàn) Websocket 廣播、點(diǎn)對(duì)點(diǎn)發(fā)送消息的代碼
- 部署Django到阿里云服務(wù)器教程示例
- Python Django搭建網(wǎng)站流程圖解
相關(guān)文章
python自動(dòng)化測(cè)試用例全對(duì)偶組合與全覆蓋組合比較
這篇文章主要為大家介紹了python自動(dòng)化測(cè)試用例全對(duì)偶組合與全覆蓋組合比較,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06Python使用matplotlib給柱狀圖添加數(shù)據(jù)標(biāo)簽bar_label()
這篇文章主要介紹了Python使用matplotlib給柱狀圖添加數(shù)據(jù)標(biāo)簽bar_label(),記錄如何用使用matplotlib給柱狀圖添加數(shù)據(jù)標(biāo)簽,是以matplotlib.pyplot.bar_label()為例,需要的朋友可以參考一下2022-03-03python?NetworkX庫生成并繪制帶權(quán)無向圖
這篇文章主要為大家介紹了python?NetworkX庫生成并繪制帶權(quán)無向圖的實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05在python3中pyqt5和mayavi不兼容問題的解決方法
今天小編就為大家分享一篇在python3中pyqt5和mayavi不兼容問題的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01pycharm進(jìn)行Git關(guān)聯(lián)和取消方式
這篇文章主要介紹了pycharm進(jìn)行Git關(guān)聯(lián)和取消方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06python時(shí)間整形轉(zhuǎn)標(biāo)準(zhǔn)格式的示例分享
這篇文章主要介紹了python時(shí)間整形轉(zhuǎn)標(biāo)準(zhǔn)格式的示例,需要的朋友可以參考下2014-02-02