6👍
✅
The {% extends %}
tag supports variables. See the doc for reference.
def my_view(request):
if request.user.is_authenicated
base_template_name = 'user_base.html'
else:
base_template_name = 'login_base.html'
# Pass base template name to the renderer
return render_to_response('your_template.html', {'base_template_name':base_template_name})
Template (please note that the value is not quoted):
{% extends base_template_name %}
...
1👍
You’re getting an error because extends
needs to be defined at the top of the template. extends
controls template inheritance: you are basically creating a subclass from some parent class, which is why extends
needs to be the first thing in the template.
Imagine writing a class, and in the __init__()
you said something like
class DoesntKnowWhereToInheritFrom(object):
def __init__():
if something:
self.inherits_from(x)
else
self.inherits_from(y)
The compiler/interpreter would freak out.
The common way to do what you are trying to do here is to check for is_authenticated
in the view
, and then render the appropriate template.
Source:stackexchange.com