Django include templates problem, from 0.96 to 1.2 - django

I using google app engine, in 0.96 I have no problem to include a template as following
{% include "../header.html" %}
However, in 1.2 the code above not functioning??
Any idea?

The reason is that google.appengine.ext.webapp.template.render doesn't use any user-configurable TEMPLATE_DIRS. Instead it invents its own TEMPLATE_DIRS byt taking the directory of the given template and using that at TEMPLATE_DIRS. This means if you call render("foo/bar/fie") it will use foo/bar as your template directory and look up files from there.
Now, the change from 0.96 til 1.2 is that the file lookup switched from using os.path.join to using django.utils._os.safe_join which does not allow escape from the base directory using ../.
I don't see any obvious way around this. It seems like you must call render with a file directly in your template directory and not in a subdirectory.

dkagedal is correct in his assessment of the problem, but there's is an easy workaround if you're not averse to monkeypatching:
try:
# Bypass Django's safe_join for template paths since App Engine sandboxes the
# filesystem anyway, and safe_join won't allow relative includes because
# webapp.template always sets the template's parent directory as the "root",
# rather than the app's real root directory.
from django.utils import _os
_os.safe_join = os.path.join
except ImportError:
pass # App is using a version of Django that doesn't use safe_join, it's OK.

It seems strange that that's not working- it is the correct syntax for the tag...
How is it not working? Not bringing in the data at all? Error message?
Is header.html just regular the body sections of the html? or is it a full standalone html page? (ie does it have html, head, body, tags etc? or just h, p, etc?)
Maybe try using the {% ssi %} tag as documented here:
SSI Template Tag

Related

(Django) How to include template file from app into base.html in templates

I am building a site in Django CMS. In my templates directory for the project is base.html.
I am writing an app "was_this_helpful" to add a dialog box on some pages for users to give feedback. I want to include a file from was_this_helpful/templates into base.html but it says the file does not exist.
{% include 'was_this_helpful/dialog.html' %}
My file structure look like this:
- was_this_helpful
- templates
- was_this_helpful
- dialog2.html
- dialog.html
- required app files
I read somewhere that sometimes template files need to be another level deeper in templates to be found which is why I made the dialog2.html but still it's not working. I do not understand how to accomplish this. Based on what I've read it should work. Is it different because I'm not in another app, just the templates directory?
Without knowing more it's hard to tell if it's a simple solution or not.
The way you have your code written, there is not a was_this_helpful/dialog.html - you only have a dialog2.html inside your was_this_helpful so was_this_helpful/dialog2.html would be the reference path.
I've always created another folder inside my templates folder with the name of the directory above my templates folder. Just like you have with your was_this_helpful second directory. I find that this makes it much easier to extend base.html files.
You can always do it absolutely too by two periods before the path call, so ../was_this_helpful/templates/dialog.html
If you don't have luck with that either, there is an {% extends %} method as well which might accomplish what you're trying to do as well.
Good luck!

django grappelli change_form.html override

I'm trying to extend django's admin change_form.html (django/contrib/admin/templates/admin/change_form.html).
What makes this more complicated for me is that I've installed grappelli, which also extends it (grappelli/templates/admin/change_form.html)
Now, I want to change it in myproject (to apply for all apps/modules in my project), and have tried to place change_form in various places but to no avail:
myproject/templates/admin/change_form.html
myproject/templates/grappelli/change_form.html
myproject/templates/admin/grappelli/change_form.html
Does anyone have a clue about where I should be placing my modified version of change_form.html in order for django to actually use it?
(any help on understanding django's search path & template extension mechanism will be appreciated).
Thanks!
You can include your template directory to TEMPLATE_DIRS
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'myapp/templates/'),
)
This way you can change the order in which Django reads your template files.

TemplateDoesNotExist error, but template does in fact exist

Vital stats:
Ubuntu 11.04
Django 1.3.1
I'm running Haystack backed by Whoosh. The rest of the site functions fine, but when I attempt to search, I get a TemplateDoesNotExist exception for a template included in templates/search/search.html. The template loader is obviously able to read search.html, or it wouldn't know to try to fetch the include. The included file, _resultPage.html is in the same directory, has the same permissions and same owner and group as search.html. And, it's not just this one include. If I comment it out, it simply errors out on the next included file.
Any ideas?
The include tag relies on django.template.loader.get_template which searches templates in normal way instead of by relative path. Do you use "_resultPage.html" or "search/_resultPage.html". If you use the first form, the absolute path of 'template/search/search' must be in TEMPLATE_DIRS. You could check by doing the following:
>>> from django.template.loader import get_template
>>> get_template('_resultPage.html')
I was under a time crunch, so I simply rolled all the included templates right into search.html and called it a day.

Cannot Extend Django 1.2.1 Admin Template

I am attempting to override/extend the header for the Django admin in version 1.2.1. However when I try to extend the admin template and simply change what I need documented here: http://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-vs-replacing-an-admin-template), I run into a recursion problem.
I have an index.html file in my project's templates/admin/ directory that starts with
{% extends "admin/index.html" %}
But it seems that this is referencing the local index file (a.k.a. itself) rather than the default Django copy. I want to extend the default Django template and simply change a few blocks. When I try this file, I get a recursion depth error.
How can I extend parts of the admin? Thanks.
SOLUTION: Rather than extending, I copied the files into my_templates_directory/admin/ and just edited them as I wished. This solution was acceptable, though not ideal.
The contrib/admin/templates/admin path needs to go before the directory with your custom admin templates in your list of paths in TEMPLATE_DIRS in your settings.py
Create a symlink to contrib/admin/templates/admin/ in your templates directory and use it in your {% extends %} statement.
cd /path/to/project/templates/
ln -s /path/to/django/contrib/admin/templates/admin/ django_admin
Now in your admin/index.html use {% extends "django_admin/index.html" %}
EDIT: Just realized that you're on windows... Not sure how to achieve the same results. Hopefully this still helps the folks on linux.
SOLUTION: Rather than extending, I copied the files into my_templates_directory/admin/ and just edited them as I wished. This solution was acceptable, though not ideal.

How to make project templates and Satchmo templates co-exist?

I'm working with a Satchmo installation that resides within an existing project. This project has its own templates as well as templates for some of the various apps that are installed. Some of these app-specific templates have their own app_base.html variations that expect to derive form base.html. I'd like to be able to do the same thing with my Satchmo templates and have them reside within my project's base, but also have some additional html added around all of them.
/templates
base.html
index.html
/news
news_base.html (extends base.html and adds news-specific template features)
index.html
detail.html
/store
base.html (overriding Satchmo's base)
This structure works somewhat, but not how I expected. in /store/base.html (Satchmo's base) I've simply replaced everything with a test message. I can see the message, so I know satchmo is loading its base and not the site's base. However, I can't extend my project's base anymore since using:
{% extends "base.html %}
Yields a recursion error since its calling itself and the following simply won't work.
{% extends "../base.html" %}
I realize that I can change my project's base.html to a slightly different name and point all app-specific templates at them, but it seems like a pretty major hack on such a fundamental aspect of the template structure.
Hmm, I didn't think django looked up templates relatively like that.
Kinda crazy hack, but this should work:
/templates/store/base.html extends "global_base.html"
/templates/global_base.html extends "base.html"
Depending on how you have your template structure set up, it might also be a good idea to play with the settings.TEMPLATE_LOADERS variable.
TEMPLATE_LOADERS Default:
('django.template.loaders.filesystem.load_template_source',
'django.template.loaders.app_directories.load_template_source')
A tuple of callables (as strings) that
know how to import templates from
various sources. See The Django
template language: For Python
programmers.
For more information on how this affects the template loading process:
http://docs.djangoproject.com/en/dev/ref/templates/api/#loader-types
From the way you describe your problem, it seems like by commenting out the "app_directories.load_template_source" file line, you might be able to better find a way to accomplish what you're doing.
django.template.loaders.app_directories.load_template_source
Loads templates from Django apps on
the filesystem. For each app in
INSTALLED_APPS, the loader looks for a
templates subdirectory. If the
directory exists, Django looks for
templates in there.
This means you can store templates
with your individual apps. This also
makes it easy to distribute Django
apps with default templates.
For example, for this setting:
INSTALLED_APPS = ('myproject.polls',
'myproject.music') ...then
get_template('foo.html') will look for
templates in these directories, in
this order:
/path/to/myproject/polls/templates/foo.html
/path/to/myproject/music/templates/foo.html
Note that the loader performs an
optimization when it is first
imported: It caches a list of which
INSTALLED_APPS packages have a
templates subdirectory.
This loader is enabled by default.
I just had this same problem. It looks like the satchmo developers planned for this by putting an "empty" base in the shop template directory. While this may not be relevant to you anymore, I would have like to have seen this here.
You can make a "shop" directory in your template directory and copy the main satchmo base.html to that directory.
This worked for me.