如何在包含模板时将列表或对象发送到子模板?
{% include "subtpl.html" with parameter={"name":"Saifullah","address":"Lahore"} %}
当我运行上面的代码时,我得到了
TemplateSyntaxError Could not parse the remainder: '{"name":"Saifullah","address":"Lahore"}' from '{"name":"Saifullah","address":"Lahore"}'
答案 0 :(得分:2)
使用Django模板无法做到这一点。
如果将对象添加到视图中的模板上下文中,则可以传递该对象,例如:
def my_view(request):
parameter = {"name":"Saifullah","address":"Lahore"}
return render(request, 'my_template.html', {'parameter': parameter}
{% include "subtpl.html" with parameter=parameter %}
或者您可以解压缩字典并使用多个关键字参数。
{% include "subtpl.html" with name="Saifullah" address="Lahore" %}