views:

609

answers:

2

The django template doc mentions the following for extending templates:

{% extends variable %}

Where do I define the variable? Is it from the views.py?

+1  A: 

Yes, it's just a context variable like any other.

You don't need to use a variable - {% extends "main.html" %} is perfectly acceptable, in fact preferable unless you need to do something massively dynamic with template inheritance.

Daniel Roseman
+2  A: 

{% extends %} actually takes a string - the location of the template to extend.

If you want to declare this variable in Python, pass it in to the template loader using your dictionary. Example:

import django.http
from django.shortcuts import render_to_response
# ...
INDEX_EXTEND = "index.html"
# ...
def response(request) :
    return render_to_response("myview.html", {'extend': INDEX_EXTEND})

And then in the view:

{% extends extend %}

Notice that 'extend' was passed in the dictionary passed to the template. You can of course define the variable anywhere else in your .py file - or even in the dictionary declaration itself.

Remember that {% extends %} can also be invoked as such:

{% extends "index.html" %}

Check out the docs on Template inheritance, too.

Lucas Jones