Django完整增刪改查系統(tǒng)實例代碼
一、效果圖如下

二、使用步驟
1.創(chuàng)建并配置一個django項目
1.1新建一個項目ch3
django-admin startproject ch3

1.2創(chuàng)建應(yīng)用employee
python manage.py startapp employee

1.3指定ch3項目的應(yīng)用程序emyployee
在setting.py中INSTALLED_APPS下加入employee應(yīng)用程序

1.4與數(shù)據(jù)庫相連
1.在數(shù)據(jù)庫中創(chuàng)建一個數(shù)據(jù)庫名為testOrm
2.在setting.py文件添加與數(shù)據(jù)庫連接的信息
3.添加模文件夾


1.5 定義與使用模型
模型定義在應(yīng)用employee的model.py文件中,并繼承model.Model類。在本次項目中定義了模型類department
class department(models.Model):
dep_name=models.CharField(max_length=32,verbose_name='部門名稱',unique=True,blank=False)
dep_script=models.CharField(max_length=60,verbose_name='備注說明',null=True
1.6生成遷移文件
python manage.py makemigrations
執(zhí)行生產(chǎn)遷移命令后會生成一下0001_initial.py文件

遷移文件生成后,使用遷移文件命令生成對應(yīng)的數(shù)據(jù)表
python manage.py migrate

二、實現(xiàn)數(shù)據(jù)庫增刪改查
2.1視圖文件
實現(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):
# 判斷請求方式,如果post,說明前端需要提交數(shù)據(jù)
if request.method=='POST':
# 獲取傳過來的get()函數(shù)中的參數(shù)(html文件input()標簽的name屬性)
dep_name=request.POST.get('dep_name')
dep_script=request.POST.get('dep_script')
# strip()過濾
if dep_name.strip()=='':
return render(request,'test_orm_old/add_dep_old.html',{'error_info':'名稱不能為空'})
# 用create()函數(shù)新建一條函數(shù),會自動保存,不需要調(diào)用save()函數(shù)
try:
# 添加數(shù)據(jù)有兩種方式:1.使用模型管理器的create()方法添加數(shù)據(jù),2.使用模型實列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':'輸入部門名稱重復(fù)或信息錯誤!'})
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前端頁面
1.在應(yīng)用下創(chuàng)建templates文件目錄用來存放前端頁面文件
2.并在templates下創(chuàng)建test_orm_old文件目錄

2.2.1主頁面(list_dep_old.xml)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>主頁面</title>
</head>
<body>
<h1>部門列表</h1>
<div><a href="/test_orm_old/add_dep_old" rel="external nofollow" >增加一條記錄</a></div>
<table border="1">
<thead>
<tr>
<td>部門名稱</td>
<td>備注說明</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">無相關(guān)記錄!</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
2.2.2增加數(shù)據(jù)頁面(add_dep_old.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>增加部門</title>
</head>
<body>
<div align="center">
<h1>增加部門</h1>
<hr>
<form action="" method="post">
<!-- 安全機制-->
{% csrf_token %}
<input type="hidden" name="id" id="id" value="{{ department.id }}">
<div>
<label>部門名稱:</label>
<input type="text" name="dep_name" id="dep_name">
</div>
<br>
<div>
<label>備注說明:</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修改頁面(edit_dep_old.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>修改部門</title>
</head>
<body>
<div align="center">
<h1>修改部門</h1>
<form action="" method="post">
{% csrf_token %}
<input type="hidden" name="id" id="id" value="{{ department.id}}">
<div>
<label>部門:</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在主項目中添加路由
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python multiprocessing 多進程并行計算的操作
這篇文章主要介紹了python multiprocessing 多進程并行計算的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
基于Tensorflow批量數(shù)據(jù)的輸入實現(xiàn)方式
今天小編就為大家分享一篇基于Tensorflow批量數(shù)據(jù)的輸入實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
如何利用Python和matplotlib更改縱橫坐標刻度顏色
對于圖表來說最簡單的莫過于作出一個單一函數(shù)的圖像,下面這篇文章主要給大家介紹了關(guān)于如何利用Python和matplotlib更改縱橫坐標刻度顏色的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-08-08
Python使用progressbar模塊實現(xiàn)的顯示進度條功能
這篇文章主要介紹了Python使用progressbar模塊實現(xiàn)的顯示進度條功能,簡單介紹了progressbar模塊的安裝,并結(jié)合實例形式分析了Python使用progressbar模塊顯示進度條的相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
Python&Matlab實現(xiàn)炫酷的3D旋轉(zhuǎn)圖
這篇文章主要為大家介紹了如何利用Python和Matlab分別實現(xiàn)酷炫的3D旋轉(zhuǎn)圖,文中的示例代碼講解詳細,感興趣的可以了解一下2022-04-04

