【发布时间】:2020-11-04 08:18:39
【问题描述】:
我有一个像这样检索 JSON 文件的视图:
json_lang = requests.get('path/to/file.json').json()
return render(request, "chat/chatroom.html", {'jsonLang': json.dumps(json_lang)})
假设 json 文件的结构有点像这样:
{
"en": {
"action.send": "Send",
"chat.joined": "joined the chatroom",
"chat.left": "left the chatroom",
...
}
如果我尝试访问像 {{ jsonLang.en.chat.joined }} 这样的模板中的其中一个字符串,我会得到一个空输出。
其他方法,例如尝试像这样jsonLang["en"]["chat.joined"] 访问它会导致错误:
TemplateSyntaxError at /chat/
Could not parse the remainder: '["en"]["chat.joined"]' from 'json_lang.["en"]["chat.joined"]'
这样做的正确方法是什么?
【问题讨论】:
-
改用
json_lang["en"]["chat.joined"]试试 -
已经试过了,我在帖子里写错了。这就是给我错误的原因