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

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

Claudiu Creanga
1#
Claudiu Creanga Published in 2018-02-13 13:16:09Z

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.

The_Cthulhu_Kid
2#
The_Cthulhu_Kid Reply to 2018-02-13 13:22:28Z

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)

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.316467 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO