SoFunction
Updated on 2024-11-13

Django receive Post request data, and save it to the database implementation methods

To say that the basic operation, everyone will basically know, but sometimes, some operations using small tips will save a lot of time.

This describes the use of dict hacks, saved to a database, used to save everyone coding effort.

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

1. The user submits a form which contains csrf.

2. The server side in addition to the csrf in the form to be filtered out, the other ones should be saved to the database.

3. See the code below for details:

The following codes are for modification and saving respectively, where modification is based on ID.

Be warned.

1. resourcesOld before saving is not the same as resourcesNew after saving and then fetching.

Especially type [get_type_display()] this method, because to escape its display, you must get resourcesNew object, otherwise you can not get the escaped, the value can only get its original value.

2. The second is to save the writing style, some people like to use T_Resources.(id=id, name=name,age=age......) , so that each time, the

But are written in this way is more cumbersome, so the following writing method, the two results are the same, of course, there is a save writing method, here will not be elaborated!

def resources(request):
  if  == 'GET':
    return render(request, 'docker/', )
  else:
    systemDict = {}
    for key in :
      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.(id=systemDict['id'])
        T_Resources.(id=systemDict['id']).update(**systemDict)
        resourcesNew=T_Resources.(id=systemDict['id'])
        result['code'] = 201
        result['message'] = 'Modified successfully'
        logInfo = "Server IP:" +  + ","
        if  != :
          logInfo += "Name:" +  + "->" +  + ','
        if  != :
          logInfo += "Type:" + resourcesOld.get_type_display() + "->" + resourcesNew.get_type_display() + ','
        if  != :
          oldLabel = list(T_Label.(type='T_Resources', value__in=).values_list('name', flat=True))[0]
          newLabel = list(T_Label.(type='T_Resources', value__in=).values_list('name', flat=True))[0]
          logInfo += "Label:" + oldLabel + "->" + newLabel + ','
        writeOperationLog(request, 1, 'Modified server successfully,' + logInfo)
      except:
        pass
      return HttpResponse((result, ensure_ascii=False))
 
    else:
      result = {'code': 401, 'message': 'Add failed!', 'data': None}
      try:
          id=T_Resources.(**systemDict).id
          resources=T_Resources.(id=id)
          result['code'] = 201
          result['message'] = 'Added successfully'
      except:
        pass
      return HttpResponse((result, ensure_ascii=False))

Above this Django Receive Post request data, and save it to the database is the realization of the method I shared with you all, I hope to be able to give you a reference, but also hope that you support me more.