Django 接收Post请求数据,并保存到数据库的实现方法
作者:豆芽胡 时间:2024-01-13 08:18:56
要说基本操作,大家基本都会,但是有时候,有些操作使用小技巧会节省很多时间。
本篇描述的就是使用dict小技巧,保存到数据库,用来节省大家编码的工作量。
主要内容:通过for循环拿到post表单中的值并保存到一个dict中,然后通过**dict保存到数据库中。
1.用户提交了一个表单,表单内容包含csrf。
2.服务端除了表单中的csrf要过滤掉,其它的都要保存到数据库中。
3.具体看下方代码:
下面的代码分别为修改和保存,其中修改是根据ID修改的。
要注意,
1.保存前的resourcesOld和保存后再获取的resourcesNew是不一样的。
尤其是type【get_type_display()】这个方法,因为要对其进行转义显示,必须获取resourcesNew对象,不然是获取不到转义后的,值只能获取其原值。
2.其次是保存的写法,有的人喜欢用T_Resources.objects.create(id=id,name=name,age=age......),这样每次,
但是都这样写比较繁琐,所以用了下面的写法,两者结果相同,当然还有一种save的写法,这里就不再阐述了!
def resources(request):
if request.method == 'GET':
return render(request, 'docker/Resources.html', )
else:
systemDict = {}
for key in request.POST:
if key != 'csrfmiddlewaretoken':
systemDict[key] = request_postData.get(key)
if 'id' in request_postData:
result = {'code': 401, 'message': '修改失败!', 'data': None}
try:
resourcesOld=T_Resources.objects.get(id=systemDict['id'])
T_Resources.objects.filter(id=systemDict['id']).update(**systemDict)
resourcesNew=T_Resources.objects.get(id=systemDict['id'])
result['code'] = 201
result['message'] = '修改成功'
logInfo = "服务器IP:" + resourcesOld.ip + ","
if resourcesOld.name != resourcesNew.name:
logInfo += "名称:" + resourcesOld.name + "->" + resourcesNew.name + ','
if resourcesOld.type != resourcesNew.type:
logInfo += "类型:" + resourcesOld.get_type_display() + "->" + resourcesNew.get_type_display() + ','
if resourcesOld.label != resourcesNew.label:
oldLabel = list(T_Label.objects.filter(type='T_Resources', value__in=resourcesOld.label).values_list('name', flat=True))[0]
newLabel = list(T_Label.objects.filter(type='T_Resources', value__in=resourcesNew.label).values_list('name', flat=True))[0]
logInfo += "标签:" + oldLabel + "->" + newLabel + ','
writeOperationLog(request, 1, '修改服务器成功,' + logInfo)
except:
pass
return HttpResponse(json.dumps(result, ensure_ascii=False))
else:
result = {'code': 401, 'message': '添加失败!', 'data': None}
try:
id=T_Resources.objects.create(**systemDict).id
resources=T_Resources.objects.get(id=id)
result['code'] = 201
result['message'] = '添加成功'
except:
pass
return HttpResponse(json.dumps(result, ensure_ascii=False))
来源:https://blog.csdn.net/u012605477/article/details/80733843
标签:Django,Post,请求,保存,数据库
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Go处理JSON数据的实现
2024-02-23 07:44:42
解决Go语言time包数字与时间相乘的问题
2023-08-06 17:07:55
Golang中的sync.WaitGroup用法实例
2023-08-31 03:57:28
Pycharm学习教程(5) Python快捷键相关设置
2021-08-08 18:56:44
![](https://img.aspxhome.com/file/2023/0/134490_0s.png)
戏说编码发展史
2022-12-12 02:48:05
go 对象池化组件 bytebufferpool使用详解
2024-02-10 14:26:11
python中遍历文件的3个方法
2022-10-21 08:41:07
MybatisPlus BaseMapper 实现对数据库增删改查源码
2024-01-26 10:03:57
![](https://img.aspxhome.com/file/2023/7/72317_0s.jpg)
关于Python 位运算防坑指南
2022-06-13 08:06:55
MySQL高可用架构之MHA架构全解
2024-01-25 03:15:23
![](https://img.aspxhome.com/file/2023/4/111364_0s.png)
pygame学习笔记(5):游戏精灵
2021-10-13 18:29:56
![](https://img.aspxhome.com/file/2023/2/86492_0s.jpg)
ORACLE 报警日志如何查看?第1/2页
2009-07-02 12:06:00
HTML5本地存储初探(三)
2010-03-07 15:49:00
![](https://img.aspxhome.com/file/UploadPic/20103/7/20100131-mheq73uiue19x7q5g9c52ngtwm-29s.png)
ASP+AJAX做类似google的搜索提示
2008-10-24 13:49:00
python 瀑布线指标编写实例
2023-04-17 02:13:34
Python实现常见数据格式转换的方法详解
2023-04-16 03:48:16
![](https://img.aspxhome.com/file/2023/0/115880_0s.png)
Requests什么的通通爬不了的Python超强反爬虫方案!
2022-02-13 18:43:45
![](https://img.aspxhome.com/file/2023/4/95194_0s.png)
Python类如何定义私有变量
2023-04-18 11:23:23
基于网格的网页设计概念及实际应用案例
2010-03-30 14:59:00
![](https://img.aspxhome.com/file/UploadPic/20103/30/1052570-42s.jpg)
MySQL数据库索引以及失效场景详解
2024-01-26 04:12:23
![](https://img.aspxhome.com/file/2023/4/82264_0s.png)