Claudiu Creanga
Claudiu Creanga

Reputation: 8366

Django: Include template with variables rendered from views.py into another template

I have a template plot.html inside app plot:

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

The plot variable is calculated from views.py inside app plot:

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

Now I want to include this template with the generated plot and other divs into another template from another app, another.html:

{% include "plot.html" %}

of course this does not generate the plot and other info from the views.py file.

I've been reading about template tags (https://docs.djangoproject.com/en/2.0/howto/custom-template-tags/), but I'm unsure what goes into poll_extras.py there or if tags is the right solution.

Upvotes: 0

Views: 1250

Answers (1)

The_Cthulhu_Kid
The_Cthulhu_Kid

Reputation: 1859

You need to pass the variable with it.

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

But you will need to pass it in from the calling view otherwise you might want to go for a tag.

(The only prevents you from copying the entire context to the other template)

Upvotes: 2

Related Questions