DJANGO:包括将变量从Views.py呈现为另一个模板的模板



我有一个模板plot.html应用程序plot

<div>
{{ plot|safe }}
</div>
some other divs here

绘图变量是根据应用程序内的views.py计算的:

class RenderView(TemplateView):
    def __init__(self):
        self.template_name = "plot.html"
    def get_context_data(self, **kwargs):
        context = super(RenderView, self).get_context_data(**kwargs)
        context['plot'] = PlotView().make_plot()
        return context

现在,我想将此模板与生成的图一起包含在另一个应用程序中的另一个模板中。html:

{% include "plot.html" %}

当然,这不会从views.py文件中生成图和其他信息。

我一直在阅读有关模板标签(https://docs.djangoproject.com/en/en/2.0/howto/custom-template-tags/(,但是我不确定在poll_extras.py中有什么标签是正确的解决方案。

您需要使用它传递变量。

{% include "plot.html" with plot=plot only %}

,但是您需要从调用view传递它,否则您可能想选择一个标签。

(only可防止您将整个context复制到其他模板(

最新更新