将对象发送到Django中{%include%}标记中的子模板

时间:2018-02-28 13:54:10

标签: python django django-templates

如何在包含模板时将列表或对象发送到子模板?

{% include "subtpl.html" with parameter={"name":"Saifullah","address":"Lahore"} %}

当我运行上面的代码时,我得到了

TemplateSyntaxError Could not parse the remainder: '{"name":"Saifullah","address":"Lahore"}' from '{"name":"Saifullah","address":"Lahore"}'

1 个答案:

答案 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" %}