views:

47

answers:

6

hi, I've already configured the necessary things to work the extends template function in django. here's my codes:

in settings.py

def my_dir():
    import os.path
    return os.path.abspath(os.path.dirname(__file__))
TEMPLATE_DIRS = ( my_dir() + '/app/templates', ) #dynamic template directory

in base.html - located in app/templates/site

....
<div id="SideBar" class="FloatLeft">
{% block sidebar %} {% endblock %}
</div>
....

in sidebar.html - located in app/templates/site

{% 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

A: 

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"

cheshire
This is not changing on the fly, it's using directory that settings.py is located in, and that only occurs when settings.py is imported.
Tom
A: 

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.

Mike Axiak
ive change it to TEMPLATE_DIRS = ( my_dir() + '/app/templates', ) but still no luck. :(
EVG
Still w8ng for answer... can anyone help me regarding this matter. im stuck in this problem :(
EVG
+1  A: 

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 %}
MovieYoda
ive tried your solution but the child template is still not showing.. Template folder is where all my html files is located it is divided into many folder, in the problem, base.html is located in site folder.
EVG
is there any settings other than the TEMPLATE_DIRS that needs to be set? i think my TEMPLATE_DIRS is working because {% include %} tags is working fine..
EVG
`TEMPLATE_DIRS` is the only place where you set the path for templates. please put all templates in one place unless you have a strong reason not to do so.
MovieYoda
i put subfolders in template folder so that i can organize my template files. under the template folder are the include folder and the site folder which holds different files. include folder handles all the include html files and site folder holds the structure template like base.html and the sidebar.html.
EVG
A: 

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',
)
zsquare
A: 

Which template are you rendering in your view? It should be the child, not the parent.

Daniel Roseman
thanks you solve my problem. i over looked that option. thanks you so much. your right that my view is pointing in the parent template.. now ive changed it to sidebar.html and now it works perfectly... :) thank you so much. :)
EVG
btw.. how to close this topic? thanks once again :)
EVG
+1  A: 

I am not sure what yout problem is, but you should check the following points :

  • The {% extends %} tage should be the first one in the template file (and put a blank line afterwards to be sure)
  • I think that the reference to the base template is relative to you TEMPLATE_DIR. Try different things like putting both templates at the same level etc.
  • Check all the tags in both templates to be sure that they are all correctly formatted
  • Check the encoding of the files. If it is UTF-8, try to disable the BOM in both files.
  • Maybe it is a problem with your directory setting. Try to hard code the absolute path to check that.

These are the problems I can imagine, but I can't guarantee that it will work.

Marc Demierre
thanks for your reply... Daniel got the correct solution. hope this topic will remind others :)
EVG