Django 接收Post请求数据,并保存到数据库的实现方法

yipeiwu_com6年前Python基础

要说基本操作,大家基本都会,但是有时候,有些操作使用小技巧会节省很多时间。

本篇描述的就是使用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))

以上这篇Django 接收Post请求数据,并保存到数据库的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python3 虚拟开发环境搭建过程(图文详解)

Python3 虚拟开发环境搭建过程(图文详解)

虚拟环境的搭建 为什么要使用虚拟环境# 1、使不同应用开发环境相互独立 2、环境升级不影响其他应用,也不会影响全局的python环境 3、防止出现包管理混乱及包版本冲突 windows平...

CentOS7.3编译安装Python3.6.2的方法

CentOS7.3编译安装Python3.6.2的方法

我使用的是 CentOS7.3 安装 Python3.6.2 1.查看是否已经安装Python Centos7 默认安装了Python2.7.5 因为一些命令要用它比如 yum 它使用...

常见的python正则用法实例讲解

下面列出Python正则表达式的几种匹配用法: 此外,关于正则的一切http://deerchao.net/tutorials/regex/regex.htm  1.测试正则表...

python向字符串中添加元素的实例方法

python向字符串中添加元素的实例方法

Python中的字符串对象是不能更改的,也即直接修改字符串中的某一位或几位字符是实现不了的,即python中字符串对象不可更改,但字符串对象的引用可更改,可重新指向新的字符串对象。 +...

解决python 无法加载downsample模型的问题

downsample 在最新版本里面修改了位置 from theano.tensor.single import downsample (旧版本) 上面以上的的import会有error...