I need to add to the output of the TemplateView html {%extends some_base.html%} in the views.py.
I can’t work with html directly, cause template_name will be always different and i don’t want to add {%extends..%} to each template.html file.
i want to do something like this:
class PageView(TemplateView):
def get_context_data(self, **kwargs):
object = PageModel.objects.get(view_base__slug=kwargs.get('slug'))
self.template_name = object.template_name
self.base='base.html'
from django.template.loader import render_to_string
#just example, it's not working
rendered = render_to_string(self.template_name)
rendered= '{% extends' + self.base + '%} '+ rendered
###
return locals()
But it does not work. Even more – i want to save all variables, which are being passed to the template.
I’m not sure why are you trying but you cannot put
{%extends ...%}in HTML (unless you want to render it again using django templates. Adding that string to template after rendering will add unwanted{%extends ...%}string in the template.But if you want you can create a template dynamically and render it. The new template can extend existing template.
For example:
More reference at: Template Compiling a string