Django完整增刪改查系統(tǒng)實(shí)例代碼
一、效果圖如下
二、使用步驟
1.創(chuàng)建并配置一個(gè)django項(xiàng)目
1.1新建一個(gè)項(xiàng)目ch3
django-admin startproject ch3
1.2創(chuàng)建應(yīng)用employee
python manage.py startapp employee
1.3指定ch3項(xiàng)目的應(yīng)用程序emyployee
在setting.py中INSTALLED_APPS下加入employee應(yīng)用程序
1.4與數(shù)據(jù)庫(kù)相連
1.在數(shù)據(jù)庫(kù)中創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)名為testOrm
2.在setting.py文件添加與數(shù)據(jù)庫(kù)連接的信息
3.添加模文件夾
1.5 定義與使用模型
模型定義在應(yīng)用employee的model.py文件中,并繼承model.Model類。在本次項(xiàng)目中定義了模型類department
class department(models.Model): dep_name=models.CharField(max_length=32,verbose_name='部門(mén)名稱',unique=True,blank=False) dep_script=models.CharField(max_length=60,verbose_name='備注說(shuō)明',null=True
1.6生成遷移文件
python manage.py makemigrations
執(zhí)行生產(chǎn)遷移命令后會(huì)生成一下0001_initial.py文件
遷移文件生成后,使用遷移文件命令生成對(duì)應(yīng)的數(shù)據(jù)表
python manage.py migrate
二、實(shí)現(xiàn)數(shù)據(jù)庫(kù)增刪改查
2.1視圖文件
實(shí)現(xiàn)增刪改查的方法
# 查詢所有數(shù)據(jù) def list_dep_old(request): # 查詢所有數(shù)據(jù) def_list=department.objects.all()#查詢方法:all(),filter(),exclude(),get() return render(request,'test_orm_old/list_dep_old.html',{'dep_list':def_list}) #添加數(shù)據(jù) def add_dep_old(request): # 判斷請(qǐng)求方式,如果post,說(shuō)明前端需要提交數(shù)據(jù) if request.method=='POST': # 獲取傳過(guò)來(lái)的get()函數(shù)中的參數(shù)(html文件input()標(biāo)簽的name屬性) dep_name=request.POST.get('dep_name') dep_script=request.POST.get('dep_script') # strip()過(guò)濾 if dep_name.strip()=='': return render(request,'test_orm_old/add_dep_old.html',{'error_info':'名稱不能為空'}) # 用create()函數(shù)新建一條函數(shù),會(huì)自動(dòng)保存,不需要調(diào)用save()函數(shù) try: # 添加數(shù)據(jù)有兩種方式:1.使用模型管理器的create()方法添加數(shù)據(jù),2.使用模型實(shí)列save()方法保存 p=department.objects.create(dep_name=dep_name,dep_script=dep_script) return redirect('/test_orm_old/list_dep_old/') except Exception as e: return render(request,'test_orm_old/add_dep_old.html',{'error_info':'輸入部門(mén)名稱重復(fù)或信息錯(cuò)誤!'}) finally: pass return render(request,'test_orm_old/add_dep_old.html/') #刪除數(shù)據(jù) def del_dep_old(request,dep_id): dep_object=department.objects.get(id=dep_id) dep_object.delete() return redirect('/test_orm_old/list_dep_old/') #修改數(shù)據(jù) def edit_dep_old(request,dep_id): if request.method=='POST': id=request.POST.get('id') dep_name=request.POST.get('dep_name') dep_script=request.POST.get('dep_script') dep_object=department.objects.get(id=id) dep_object.dep_name=dep_name dep_object.dep_script=dep_script dep_object.save() return redirect('/test_orm_old/list_dep_old/') else: dep_object=department.objects.get(id=dep_id) return render(request,'test_orm_old/edit_dep_old.html',{'department':dep_object})
2.2前端頁(yè)面
1.在應(yīng)用下創(chuàng)建templates文件目錄用來(lái)存放前端頁(yè)面文件
2.并在templates下創(chuàng)建test_orm_old文件目錄
2.2.1主頁(yè)面(list_dep_old.xml)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>主頁(yè)面</title> </head> <body> <h1>部門(mén)列表</h1> <div><a href="/test_orm_old/add_dep_old" rel="external nofollow" >增加一條記錄</a></div> <table border="1"> <thead> <tr> <td>部門(mén)名稱</td> <td>備注說(shuō)明</td> <td colspan="2">操作</td> </tr> </thead> <tbody> {% for dep in dep_list %} <tr> <td>{{ dep.dep_name }}</td> <td>{{ dep.dep_script}}</td> <td><a href="/test_orm_old/del_dep_old/{{dep.id}}/" rel="external nofollow" >刪除</a> </td> <td><a href="/test_orm_old/edit_dep_old/{{dep.id}}/" rel="external nofollow" >修改</a> </td> </tr> {% empty %} <tr> <td colspan="4">無(wú)相關(guān)記錄!</td> </tr> {% endfor %} </tbody> </table> </body> </html>
2.2.2增加數(shù)據(jù)頁(yè)面(add_dep_old.html)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>增加部門(mén)</title> </head> <body> <div align="center"> <h1>增加部門(mén)</h1> <hr> <form action="" method="post"> <!-- 安全機(jī)制--> {% csrf_token %} <input type="hidden" name="id" id="id" value="{{ department.id }}"> <div> <label>部門(mén)名稱:</label> <input type="text" name="dep_name" id="dep_name"> </div> <br> <div> <label>備注說(shuō)明:</label> <input type="text" name="dep_script" id="dep_script"> </div> <br> <div> <input type="submit" value="保存"> </div> </form> {{ error_info }} </div> </body> </html>
2.2.3修改頁(yè)面(edit_dep_old.html)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>修改部門(mén)</title> </head> <body> <div align="center"> <h1>修改部門(mén)</h1> <form action="" method="post"> {% csrf_token %} <input type="hidden" name="id" id="id" value="{{ department.id}}"> <div> <label>部門(mén):</label> <input type="text" name="dep_name" id="dep_name" value="{{ department.dep_name }}"> </div> <br> <div> <label>備注:</label> <input type="text" name="dep_script" id="dep_script" value="{{ department.dep_script}}"> </div> <br> <div><input type="submit" value="保存"></div> </form> {{ error_info }} </div> </body> </html>
2.3在主項(xiàng)目中添加路由
from django.contrib import admin from django.urls import path,include from employee import views urlpatterns = [ path('admin/', admin.site.urls), # path('',include('employee.urls')), path('test/',views.test), path('list_dep_old/',views.list_dep_old), path('test_orm_old/list_dep_old/',views.list_dep_old), path('test_orm_old/add_dep_old/',views.add_dep_old), path('test_orm_old/del_dep_old/<int:dep_id>/',views.del_dep_old), ]
總結(jié)
注意一定要配置好每一步。
到此這篇關(guān)于Django完整增刪改查系統(tǒng)的文章就介紹到這了,更多相關(guān)Django增刪改查系統(tǒng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python標(biāo)準(zhǔn)日志模塊logging的使用方法
python的標(biāo)準(zhǔn)庫(kù)里的日志系統(tǒng)從Python2.3開(kāi)始支持。只要import logging這個(gè)模塊即可使用。2013-11-11python multiprocessing 多進(jìn)程并行計(jì)算的操作
這篇文章主要介紹了python multiprocessing 多進(jìn)程并行計(jì)算的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03基于Tensorflow批量數(shù)據(jù)的輸入實(shí)現(xiàn)方式
今天小編就為大家分享一篇基于Tensorflow批量數(shù)據(jù)的輸入實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02如何利用Python和matplotlib更改縱橫坐標(biāo)刻度顏色
對(duì)于圖表來(lái)說(shuō)最簡(jiǎn)單的莫過(guò)于作出一個(gè)單一函數(shù)的圖像,下面這篇文章主要給大家介紹了關(guān)于如何利用Python和matplotlib更改縱橫坐標(biāo)刻度顏色的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08Python實(shí)現(xiàn)簡(jiǎn)單登錄驗(yàn)證
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)簡(jiǎn)單登錄驗(yàn)證的相關(guān)資料,需要的朋友可以參考下2016-04-04python實(shí)現(xiàn)kmp算法的實(shí)例代碼
這篇文章主要介紹了python實(shí)現(xiàn)kmp算法的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04Python使用progressbar模塊實(shí)現(xiàn)的顯示進(jìn)度條功能
這篇文章主要介紹了Python使用progressbar模塊實(shí)現(xiàn)的顯示進(jìn)度條功能,簡(jiǎn)單介紹了progressbar模塊的安裝,并結(jié)合實(shí)例形式分析了Python使用progressbar模塊顯示進(jìn)度條的相關(guān)操作技巧,需要的朋友可以參考下2018-05-05機(jī)器學(xué)習(xí)10大經(jīng)典算法詳解
這篇文章主要為大家詳細(xì)介紹了機(jī)器學(xué)習(xí)10大經(jīng)典算法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Python&Matlab實(shí)現(xiàn)炫酷的3D旋轉(zhuǎn)圖
這篇文章主要為大家介紹了如何利用Python和Matlab分別實(shí)現(xiàn)酷炫的3D旋轉(zhuǎn)圖,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-04-04