TypeError: unhashable type: 'dict' Django

Cos*_*tin 2 python django dictionary

我有一个非常简单的字典,上面有一些数据:

some_data= {'totalsForAllResults': {'ga:sessions': '11'}, 'profileInfo': {'internalWebPropertyId': '104132673', 'accountId': '67836206', 'profileName': 'My New Cool Site', 'webPropertyId': 'UA-677293506-1', 'profileId': '108628346', 'tableId': 'ga:108372846'},
Run Code Online (Sandbox Code Playgroud)

我的观点是:

sessions = some_data['totalsForAllResults']['ga:sessions']
account_id = some_data['profileInfo']['accountId']
property_id = some_data['profileInfo']['internalWebPropertyId']
property_name = some_data['profileInfo']['profileName']
print(sessions,account_id,property_id,property_name)
return render(request, 'ga_app/report.html', {'sessions':sessions},
                                             {'account_id':account_id},
                                             {'property_id':property_id},
                                             {'property_name':property_name},)
Run Code Online (Sandbox Code Playgroud)

变量完美地打印在我的外壳上,但是 django 不想将它们传递给模板,我不断收到TypeError: unhashable type: 'dict'但我将变量发送到模板而不是字典。为什么会发生这种情况?

nik*_*k_m 5

改变这个:

return render(request, 'ga_app/report.html', {'sessions':sessions},
                                             {'account_id':account_id},
                                             {'property_id':property_id},
                                             {'property_name':property_name},)
Run Code Online (Sandbox Code Playgroud)

对此:

return render(request, 'ga_app/report.html', {'sessions': sessions,
                                              'account_id': account_id,
                                              'property_id':property_id,
                                              'property_name':property_name}
)
Run Code Online (Sandbox Code Playgroud)

您必须传递一个 dict 作为上下文。您正在通过其中的 4 个!