【问题标题】:Share context with included template django与包含的模板 django 共享上下文
【发布时间】:2014-04-16 15:13:19
【问题描述】:

是否可以与 django 中包含的模板共享上下文?例如,假设我们有:

base.html 

<html>
    <head>...<head>
    <body>
        {% block content %}{% endblock %}
    </body>

mypage.html

{% extends 'base.html' %}
{% block content %}
    Hi this is my template ! 
    {% include 'my_fragment.html' %}
{% endblock %}


my_fragment.html

<div> I want to use the context here ! </div>

我的观点是调用模板mypage.html。我不能调用 make my_fragment.html extends mypage.html 因为它只是我页面的一小部分。如何在不发送视图的所有参数的情况下做到这一点:

{% include 'my_fragment.html' with somevar=myvar %}

谢谢!

【问题讨论】:

    标签: django templates inheritance include


    【解决方案1】:

    我相信它默认包含在内。 Docs

    包含的模板在包含它的模板的上下文中呈现。

    【讨论】:

    • 你说得对,我错过了这一点。包含参数时,我总是使用with。谢谢,我的代码现在会更干净了!
    猜你喜欢
    • 2011-01-07
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-07
    • 1970-01-01
    相关资源
    最近更新 更多