Skip to content Skip to sidebar Skip to footer

Error: Unhashable Type: 'dict'

i have a problem with Django: I can't show the data from mysql database in the table. I see the error 'Exception Value: unhashable type: 'dict'' This is my code: views.py: List_of_

Solution 1:

You are passing bad arguments to HttpResponse constructor signature is

HttpResponse.__init__(content='', content_type=None, status=200, reason=None, charset=None)

and I thinks you want to use {'List_of_date': List_of_date} as context for template render. So you rather want call something like (i don't know what menas your args variable)

return HttpResponse(template.render(Context({'List_of_date': List_of_date})))

Solution 2:

When do you generally get an unhashable type: 'dict' error?

When you try to use a dictionary as a key to perform lookup in another dictionary, you get this error.

For example:

In [1]: d1 = {'a':1, 'b':2}

In [2]: d2 = {'c':3}

In [3]: d2[d1] # perform lookup with key as dictionary 'd1'---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-163d2a314f4b> in <module>()
----> 1 d2[d1]

TypeError: unhashable type: 'dict'

Why are getting this error?

This is because you have passed wrong arguments to HttpResponse class when creating its instance as pointed out by @Francis.

When you do HttpResponse(template.render(context), args, {'List_of_date': List_of_date}), then template.render(context) becomes content, args becomes the content_type and the dictionary {'List_of_date': List_of_date} becomes the status of the response object.

Now internally, Django performs a lookup based on the status of the response object to set the reason_phrase on response object. Since the status is not an integer but a dictionary, the above error occurs.

Answer :

You need to use render() shortcut provided by Django instead as @Daniel also mentioned which is perfect for what you are intending to do.

It will render the template for you and automatically render the template with RequestContext instance. You can do something like:

return render(template_name, {'List_of_date': List_of_date})

Post a Comment for "Error: Unhashable Type: 'dict'"