django QueryDict only returns the last value of a list

后端 未结 3 1588
鱼传尺愫
鱼传尺愫 2021-02-12 09:20

Using django 1.8, I\'m observing something strange. Here is my javascript:

function form_submit(){
  var form = $(\'#form1_id\');
  request = $.post($(this).attr         


        
3条回答
  •  臣服心动
    2021-02-12 10:20

    The function below converts a QueryDict object to a python dictionary. It's a slight modification of Django's QueryDict.dict() method. But unlike that method, it keeps lists that have two or more items as lists.

    def querydict_to_dict(query_dict):
        data = {}
        for key in query_dict.keys():
            v = query_dict.getlist(key)
            if len(v) == 1:
                v = v[0]
            data[key] = v
        return data
    

    Usage:

    data = querydict_to_dict(request.POST)
    
    # Or
    
    data = querydict_to_dict(request.GET)
    

提交回复
热议问题