It is possible to pass a variable to extends
. From django book:
In most cases, the argument to {%
extends %} will be a string, but it
can also be a variable, if you don’t
know the name of the parent template
until runtime. This lets you do some
cool, dynamic stuff.
However, do note that there is a caveat!
If you use {% extends %} in a
template, it must be the first
template tag in that template.
Otherwise, template inheritance won’t
work.
This means you'll have to place the page selection logic in your views (and not in your template as that will come before the extend
tag hence invalidating template inheritance) and pass in the variable in your context. See this answer for more details.
Update
In your updated question, your code is almost there, except that the first argument to render_to_response should be an template filename, not a directory.
I would rewrite what you have as:
if login:
sub_page = '/p_change/newpage.html'
else:
sub_page = '/p_h/newpage1.html'
# 'base.html' being the base template containing {% extends page %}
return render_to_response('base.html', context_instance=RequestContext(request, {
'page' : sub_page,
}))
Of course, I'm just speculating on what you're trying to achieve and may well be mistaken.