網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
一、效果圖如下
二、使用步驟
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='部門名稱',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':'輸入部門名稱重復(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>部門列表</h1>
<div><a href="/test_orm_old/add_dep_old" >增加一條記錄</a></div>
<table border="1">
<thead>
<tr>
<td>部門名稱</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}}/" >刪除</a> </td>
<td><a href="/test_orm_old/edit_dep_old/{{dep.id}}/" >修改</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>增加部門</title>
</head>
<body>
<div align="center">
<h1>增加部門</h1>
<hr>
<form action="" method="post">
<!-- 安全機(jī)制-->
{% 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>備注說(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>修改部門</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在主項(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é)
注意一定要配置好每一步。
原文鏈接:https://blog.csdn.net/m0_58877630/article/details/123855277
相關(guān)推薦
- 2022-12-22 React?Hook?-?自定義Hook的基本使用和案例講解_React
- 2022-05-11 解決Spring Boot報(bào)錯(cuò)Mapped Statements collection alread
- 2022-09-20 Python?flask使用ajax上傳文件的示例代碼_python
- 2023-05-06 pandas中g(shù)roupby操作實(shí)現(xiàn)_python
- 2022-04-22 electron設(shè)置最小大小和最大大小
- 2023-01-03 C++特性之智能指針shared_ptr詳解_C 語(yǔ)言
- 2022-10-10 scrapy框架ItemPipeline的使用_python
- 2022-03-28 c語(yǔ)言經(jīng)典習(xí)題之逆序字符串詳解_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支