Reputation: 285
I've already configured the necessary things to work the extends template function in django. here's my codes:
def my_dir():
import os.path
return os.path.abspath(os.path.dirname(__file__))
TEMPLATE_DIRS = ( my_dir() + '/app/templates', ) #dynamic template directory
....
<div id="SideBar" class="FloatLeft">
{% block sidebar %} {% endblock %}
</div>
....
{% extends "site/base.html" %}
{% block sidebar %}
some code here
{% endblock %}
I've tried also the {% include "site/sidebar.html"%} tag in the base.html to check the template directory, and yes include tag is working...
what's the problem in the {% extends %} ? why does it doesnt detect its parent template..
please help me guys.. your help is greatly appreciated... im still waiting for the answer.. tnx
Upvotes: 8
Views: 10505
Reputation: 401
The answer Daniel Roseman gave is spot on, but there is a quick and easy way around this if pointing to your child template is not practical (as it might not be with more complex projects).
In your child template, remove the {% extends "" %} tags you have that are pointing to your parent.
In your parent template, replace {% block content %} with {% include "path/to/child/template" %}
That's it! Your child template will now load into the block content exactly as if you had rendered it directly.
Upvotes: 2
Reputation: 1108
I am not sure what yout problem is, but you should check the following points :
These are the problems I can imagine, but I can't guarantee that it will work.
Upvotes: 2
Reputation: 599610
Which template are you rendering in your view? It should be the child, not the parent.
Upvotes: 16
Reputation: 10146
Are you sure you have the proper template loaders setup? You should have this in your settings.py:
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
Upvotes: 1
Reputation: 73608
Use os.path.join
to combine 2 directories.
import os.path
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__), 'templates'),
)
Here I am assuming that templates
is a directory where you keep your templates. Now, to access the templates this is the base directory for them. So to extend base.html
in some other file do like this -
{% extends "base.html" %}
...
{% endblock %}
Upvotes: 1
Reputation: 12004
Your main issue is that you're forgetting a comma in the TEMPLATE_DIRS setting. Try this:
TEMPLATE_DIRS = ( my_dir() + '/app/templates', )
Please disregard cheshire's answer.
Upvotes: 1
Reputation: 2124
There are a lot of problems. The short answer is "No, you can't change template dirs on-the-fly, and even if you could, you would do it definitely not the way you're doing it now"
Upvotes: 1