从 django 模板中的字典键中获取值

Get value from a dictionary key in django template

我需要从传递给 django 模板的字典的特定键中获取值。类似于:

...    
a = "string1"
b = "string2"
dic = {'a':a, 'b':b}
return render(request, 'index.html', {...,
'dic':dic})

在模板中,不应该{{ dic.a }} return string1吗?

试试这个

a = "string1"
b = "string2"
dic = {'a':a, 'b':b}
return render(request, 'index.html', dic)

然后你可以在你的模板中直接使用{{a}}获取它。