0

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.

Feanor
  • 3,568
  • 5
  • 29
  • 49
  • 1
    possible duplicate: http://stackoverflow.com/questions/1331148/how-do-i-use-djangos-template-extends-variable – Hedde van der Heide Sep 11 '12 at 07:59
  • no. i want to to add {% extends %} string to the output html, i don't want to add this manually in the template. – Feanor Sep 11 '12 at 08:10
  • 2
    What? You want the actual *raw string* `{% extends %}` to appear in your rendered output? – Daniel Roseman Sep 11 '12 at 08:22
  • yeah, this is what i want. and i want it to be processed by django and used to extend base.html – Feanor Sep 11 '12 at 08:25
  • But that's the opposite of what I said! *Either* you want the raw string in the rendered output, *or* you want it as part of the template to be processed by Django. Which? – Daniel Roseman Sep 11 '12 at 08:27
  • I want it to be the part of Django processed – Feanor Sep 11 '12 at 08:30
  • Take a look at http://pypi.python.org/pypi/django-dbtemplates. You could use this and just directly modify the values in the DB, or use it's code as a base to write your own template loader to modify them on the fly. – Barnaby Shearer Sep 11 '12 at 08:36

2 Answers2

1

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:

>>> from django.template import Template, Context
>>> #creates a template from string, "base.html" can be self.base in your case
>>> t = Template('{%extends "' + "base.html" + '"%} ...') 
>>> c = Context({'your_var1': 'var1_value'})            #get context for template
>>> t.render(c)                                         #render the created template 
u'\n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n
 <html xmlns="http://www.w3.org/1999/xhtml">
....

More reference at: Template Compiling a string

Rohan
  • 52,392
  • 12
  • 90
  • 87
0

The same you can achieve in django template by passing a variable template_name to template. And then in template put this code at very top.

{% with template_name|add:".html" as template %}
{% include template %}
{% endwith %}

Or see this question for more help.

Community
  • 1
  • 1
Ahsan
  • 11,516
  • 12
  • 52
  • 79