我有一个非常简单的字典,上面有一些数据:
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'},
我的观点:
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},)
变量在我的shell上完美打印,但是django不想将它们传递给模板,我不断得到TypeError: unhashable type: 'dict'
但是我将变量发送到模板而不是字典。为什么会这样?
答案 0 :(得分:2)
改变这个:
return render(request, 'ga_app/report.html', {'sessions':sessions},
{'account_id':account_id},
{'property_id':property_id},
{'property_name':property_name},)
到此:
return render(request, 'ga_app/report.html', {'sessions': sessions,
'account_id': account_id,
'property_id':property_id,
'property_name':property_name}
)
你必须传递一个dict作为上下文。你传递了4个!