TemplateDoesNotExist error, but template does in fact exist - django

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.

Related

variable in setting file using in template

everybody! I want to access to constants value that I declare in my setting.py file to use in my template, but not in a template inside the app, I just want to use in my home template the template that I declare in my setting file.
CMS_TEMPLATES = (
## Customize this
('page.html', 'Page'),
('feature.html', 'Page with Feature'),
('homeTemplate.html', 'Home Template') // I want to use here
)
I found this example about the same problem in StackOverflow, but all case uses the variable inside apps not in top level.
How is the best way to use this value inside this template!!
I'd start by asking - why do you want to do that? It seems like an odd way of... adjusting the templates directories that you're using, i guess??
I wonder if you may have an XY problem. If you post what you're trying to achieve, that might be better than a specific solution to this specific problem.
Broadly though, you can only serve django variables (including from settings.py) inside a project or script that's using Django, or at least its environments. Doing from django.conf import settings imports the settings that are available at the highest level in the project, across all of the apps. Projects are laid out in this rough fashion.
myproject -
- myapp1
- myapp2
- myproject
- settings.py
- wsgi.py
- manage.py
As in this answer, For external scripts, you can still import the django settings thus:
import os
import django
from django.conf import settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
django.setup()
# now you can use settings.VARIABLE_NAME
But: this still won't get it into a template, because getting the variable from here into a template needs you to use the django engine to render a response.
If it's not being served by the django system, it's not a template, it's just a regular html file.
If it's a template in another app in your overall project, you would pass it as part of your context dictionary, as laid out in the docs on templates.

assertTemplateUsed with django_jinja tries to match full path

I've recently upgraded to Django 1.8, replacing the jingo adapter with django_jinja for working with Jinja2 templates. I have a few tests that look like this:
self.assertTemplateUsed(response, 'staff/update_record.jinja')
These used to work, but now they fail because it seems to be comparing my string with the full path of the template rather than relative to the template directory:
AssertionError: Template 'staff/update_record.jinja' was not a template used to render the response. Actual template(s) used: /home/ben/Repos/myrepo/myproject/templates/staff/update_record.jinja
Is there something I need to add in settings.TEMPLATES or anywhere else to make this return the relative path rather than the absolute path?

Error when trying to load Django custom filters into template

I've inherited a Django application that I need to modify using a custom template filter. I'm absolutely new to Django and am quite mystified by it. I thought I had followed the instructions exactly, and also followed all the advice from other posts on the subject, but I still get an error when I include the following line in my template:
{% load mlgb_custom_filters %}
My directory structure is as follows:
mysite (i.e. the project)
__init__.py
mlgb/ (i.e. the app)
__init__.py
templatetags/
__init__.py
mlgb_custom_filters.py
The code of mlgb_custom_filters.py is as follows:
from django import template
from django.template.defaultfilters import stringfilter
register = template.Library()
#register.filter(name='fix_dashes')
#stringfilter
def fix_dashes( value ):
return value.replace( '--', 'DASH' )
if __name__ == "__main__":
testvar = fix_dashes( "ouch -- ow -- I hate django" )
print testvar
As you can see, I've added a 'name = main' section to let me run it in standalone mode, just to check that there are no errors in that particular file, and it's fine when run in standalone mode.
Based on someone else's advice, I've also tried importing it into another file, just to see whether there was an import error, and once again, it was fine if I added this to the end of settings.py (while using the dev server):
try:
import mlgb.templatetags.mlgb_custom_filters
except Exception, exc:
print 'error importing mlgb_custom_filters'
print exc
Also, INSTALLED_APPS in settings.py includes the line 'mysite.mlgb', and I have also tried putting just 'mlgb' instead of 'mysite.mlgb' there, as yet another person suggested. And I restarted the dev server every time I made a change.
I think I have tried every single suggestion that I have found on the web until now. Does anyone have any new ideas? Could it be anything to do with the fact that I have inherited a directory structure where the template directory is not within the same structure as the application, i.e. it is not under mysite? Scraping the barrel for ideas here! I hope someone can help.
OK, in the situation that I was in when first posting this question, it seems all I actually needed to do was touch a wsgi file under my appname/apache directory to force the application to be refreshed. Yesterday's initial answer was a red herring. Basically I should have touched the file myproject/myapp/apache/myapp.wsgi. Then maybe restart Apache for good measure? But the confusion was caused by the fact that apparently it wasn't enough either simply to restart Apache or to manually recompile the Python. In order to pick up my changes, seems like I needed to touch that wsgi file. Then all is well.
I can now post an answer to my own question thanks to the help of my colleague Masud Khokhar, whose brilliant detective work has saved the day. To recap, my application worked fine until I added a 'load' statement to one of my template files, to load a 'custom filters' module. Masud identified that now I needed to use a full/absolute path to the template file in urls.py instead of a relative one as I had before (and which worked before, until it needed to load the custom filters module). So, in urls.py, I had a section of code as follows:
url(r'^book/(?P<object_id>\d+)/$', 'list_detail.object_detail',
kwargs={
'queryset':Book.objects.all(),
'template_name' : 'mlgb/mlgb_detail.html'
},
name='mlgb_detail'
),
Instead of this:
'template_name' : 'mlgb/mlgb_detail.html'
I needed something like this:
'template_name' : '/THE_FULL_PATH/mlgb/templates/mlgb/mlgb_detail.html'
Made that change - sorted! Thank you once again, Masud.

How does django know how to find templates? (1.2x)

Running through the djangobook (ver 2). I had a little trouble with template loading; my relevant filestructure:
testSite/urls.py
testSite/books/views.py
testSite/books/templates/
testSite/contact/views.py
testSite/contact/templates/
When I set-up a view for the books chapter (chap. 5), I was able to create a url in urls.py, point it to a view function in testSite/books/views, but when I called the template from that view function, I did not have to specify a directory - django knew it was in testSite/books/templates.
I tried doing the same thing for the contact form chapter (chap. 7), but this time it would not load the template - I had to go back to settings.py and explicitly place testSite/contact/templates into TEMPLATE_DIRS:
# testSite/settings.py
# ....
TEMPLATE_DIRS = (
'/home/chris/djcode/testSite/templates',
'/home/chris/djcode/testSite/contact/templates',
)
So - is there an obvious explanation as to why I need to point django to the contact/templates folder, but not the books/templates folder?
(If not, I can post more code - trying to keep it short)
You either did not add the contact application to your INSTALLED_APPS in settings.py OR you are trying to load the template from one application inside another application. The TEMPLATE_DIRS is where to look if it doesn’t find the template inside the same application as the views are loading from.
Probably because a Template Loader knows how to find it. The app_directories one will find the templates directory in each application.
django.template.loaders.filesystem.Loader will use the TEMPLATE_DIRS setting.
django.template.loaders.app_directories.Loader will find the templates directory in your installed apps.
If the first loader in the TEMPLATE_LOADERS setting can't find it, Django will ask the next to see if it can find it.
How are you telling your views which templates you're using?
I expect your books app is in INSTALLED_APPS, but contact is not.

Django include templates problem, from 0.96 to 1.2

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