Implementation method of Django receiving data requested by Post and saving it to database

  • 2021-07-18 08:14:36
  • OfStack

When it comes to basic operations, everyone can basically do them, but sometimes, some operations can save a lot of time by using small skills.

This article describes the use of dict tips, saved to the database, to save everyone coding workload.

Main content: Get the value in post form through for loop and save it to 1 dict, and then save it to database through **dict.

1. The user submits a form containing csrf.

2. In addition to the form in the server csrf to filter out, the rest of the database to save.

3. Look at the code below:

The following code is modified and saved respectively, where the modification is modified according to ID.

Note that,

1. The resourcesOld before saving is different from the resourcesNew obtained after saving.

Especially the method of type "get_type_display ()", because to escape it, you must get the resourcesNew object, otherwise you can't get the escaped value, and you can only get its original value.

2. The second is the writing of preservation. Some people like to use T_Resources. objects. create (id=id, name=name, age=age......), so that every time,

However, it is cumbersome to write in this way, so the following writing method is used, and the results are the same. Of course, there is also a writing method of save, which will not be described here!


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': ' Modification failed! ', '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'] = ' Successful modification '
        logInfo = " Server IP : " + resourcesOld.ip + " , "
        if resourcesOld.name != resourcesNew.name:
          logInfo += " Name: " + resourcesOld.name + "->" + resourcesNew.name + ' , '
        if resourcesOld.type != resourcesNew.type:
          logInfo += " Type: " + 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 += " Label: " + oldLabel + "->" + newLabel + ' , '
        writeOperationLog(request, 1, ' The server was modified successfully, ' + logInfo)
      except:
        pass
      return HttpResponse(json.dumps(result, ensure_ascii=False))
 
    else:
      result = {'code': 401, 'message': ' Add failed! ', 'data': None}
      try:
          id=T_Resources.objects.create(**systemDict).id
          resources=T_Resources.objects.get(id=id)
          result['code'] = 201
          result['message'] = ' Successful addition '
      except:
        pass
      return HttpResponse(json.dumps(result, ensure_ascii=False))

Related articles: