Error: “dictionary update sequence element #0 has length 1; 2 is required” on Django 1.4

后端 未结 14 1077
梦如初夏
梦如初夏 2020-11-29 21:43

I have an error message on django 1.4:

dictionary update sequence element #0 has length 1; 2 is required

[EDIT]

It happe

相关标签:
14条回答
  • 2020-11-29 22:27

    I too had a similar type of problem . The solution is simple . just dont try to enter NULL or None value in values or u might have to use Something like this
    dic.update([(key,value)])

    0 讨论(0)
  • 2020-11-29 22:29

    I got this error when I was messing around with string and dictionary.

    dict1 = {'taras': 'vaskiv', 'iruna': 'vaskiv'}
    str1 = str(dict1)
    dict(str1)
    *** ValueError: dictionary update sequence element #0 has length 1; 2 is required
    

    So what you actually got to do to get dict from string is:

    dic2 = eval(str1)
    dic2
    {'taras': 'vaskiv', 'iruna': 'vaskiv'}
    

    Or in matter of security we can use literal_eval

    from ast import literal_eval
    
    0 讨论(0)
  • 2020-11-29 22:29

    I faced the above mentioned problem when I forgot to pass a keyword argument name to url() function.

    Code with error

     url(r"^testing/$", views.testing, "testing")
    

    Code without error

    url(r"^testing/$", views.testing, name="testing")
    

    So finally I removed the above error in this way. It might be something different in your case. So check your url patterns in urls.py.

    0 讨论(0)
  • 2020-11-29 22:30

    Solution»

    Pass a keyword argument name with value as your view name e.g home or home-view etc. to url() function.

    Throws Error»

    url(r'^home$', 'common.views.view1', 'home'),

    Correct»

    url(r'^home$', 'common.views.view1', name='home'),

    0 讨论(0)
  • 2020-11-29 22:31

    The error should be with the params. Please verify that the params is a dictionary object. If it is just a list/tuple of arguments use only one * (*params) instead of two * (**params). This will explode the list/tuple into the proper amount of arguments.

    Or, if the params is coming from some other part of code as a JSON file, please do json.loads(params), because the JSON objects sometimes behave as string and so you need to make it as a JSON using load from string (loads).

    super(HStoreDictionary, self).__init__(value, **params)
    

    Hope this helps!

    0 讨论(0)
  • 2020-11-29 22:37

    I encountered this issue when trying to invoke the update method with a parameter of a wrong type. The expected dict was:

    {'foo': True}
    

    The one that was passed was:

    {'foo': "True"}
    

    make sure you check all the parameters you pass are of the expected type.

    0 讨论(0)
提交回复
热议问题