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

后端 未结 14 1075
梦如初夏
梦如初夏 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:17

    I got the same issue and found that it was due to wrong parameters. In views.py, I used:

    return render(request, 'demo.html',{'items', items})    
    

    But I found the issue: {'items', items}. Changing to {'items': items} resolved the issue.

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

    Just ran into this problem. I don't know if it's the same thing that hit your code, but for me the root cause was because I forgot to put name= on the last argument of the url (or path in Django 2.0+) function call.

    For instance, the following functions throw the error from the question:

    url(r'^foo/(?P<bar>[A-Za-z]+)/$', views.FooBar.as_view(), 'foo')
    path('foo/{slug:bar}/', views.FooBar, 'foo')
    

    But these actually work:

    url(r'^foo/(?P<bar>[A-Za-z]+)/$', views.FooBar.as_view(), name='foo')
    path('foo/{slug:bar}/', views.FooBar, name='foo')
    

    The reason why the traceback is unhelpful is because internally, Django wants to parse the given positional argument as the keyword argument kwargs, and since a string is an iterable, an atypical code path begins to unfold. Always use name= on your urls!

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

    I hit this error calling:

    dict(my_data)
    

    I fixed this with:

    import json
    
    json.loads(my_data)
    
    0 讨论(0)
  • 2020-11-29 22:24

    You are sending one parameter incorrectly; it should be a dictionary object:

    • Wrong: func(a=r)

    • Correct: func(a={'x':y})

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

    In my case, my get_context_data in one of my views was returning return render(self.request, 'es_connection_error.html', {'error':error}); in a try/catch block instead of returning context

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

    Here is how I encountered this error in Django and fixed it:

    Code with error

    urlpatterns = [path('home/', views.home, 'home'),]
    

    Correction

    urlpatterns = [path('home/', views.home, name='home'),]
    
    0 讨论(0)
提交回复
热议问题