assertTemplateUsed with django_jinja tries to match full path - django

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?

Related

CakePHP 3.x, How to get full path to a template file

I've tried $this->viewBuilder()->templatePath(), but this only returns the prefix and controller name. (ex. Dashboard/Users)
The full path is more like /usr/local/var/www/mysite/vendor/vendorname/users/src/Template/Dashboard/Users
I've tried a few other things like Plugin::path($this->viewBuilder()->plugin()) to get part of that path, but I have yet to find any piece of code that will return the settings for what the src folder is called and what the Template folder is called.
I could hard code them as 'src' . DS . 'Template', but was hoping I'd find something that would work even if those were changed through some config setting somewhere. (Ideally there would just be a viewBuilder->absoluteTemplatePath() or something like it.)
You can retrieve possible template paths via App::path().
If you want to retrieve the template path for your Users plugin, then you could do
$templatesPath = current(\Cake\Core\App::path('Template', 'Users'));
This would give you something like
/usr/local/var/www/mysite/vendor/vendorname/users/src/Template/
It should be noted that this method doesn't necessarily return only a single path, it does so for plugin templates though.
If you need the path to an actual file, then you'll have to concatenate the remaining path segments on your own.
See also
API > \Cake\Core\App::path()

django 1.7 template deleted, but still loads as if it exists

Question:
What do I need to do to have Django load a template from the filesystem, instead of loading from memory/cache?
Use case:
I am using render_to_string() to generate a template's HTML for use as an attachment to a EmailMultiAlternatives.
Problem:
I have made changes to the template, sent a subsequent email, yet the changes to the template are not reflected in the email. I have printed the render_to_string() prior to sending the email and it too is not updated, so the problem exists prior to the email part.
Details:
I am using the default settings.TEMPLATE_LOADERS (filesystem/app_directories) for Django 1.7, have tried clearing the cache and have restarted the default development server.
If I change the filename of the template, render_to_string() renders the "changes."
What is even stranger is I have completely removed the template file from the filesystem, yet render_to_string() still renders the original template.
This is a "new problem," as I have modified templates countless times and their changes have been reflected immediately.
Maybe you have that file somewhere else on your file system? Try checking your template file locations in settings.py and review your template files in your file system.

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.

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

Dynamically add to TEMPLATE_DIRS at runtime with a Django project

For a Django project I'm working on, I need to be able to allow the user to specify the path used in TEMPLATE_DIRS. This is to implement selectable "themes". For example:
TEMPLATE_DIRS = (
os.path.join(WEBSITE_ROOT, 'templates', THEME_NAME).replace('\\', '/'),
os.path.join(WEBSITE_ROOT, 'templates', 'default').replace('\\', '/'),
)
But the THEME_NAME variable should come from the database via the site administration.
Any ideas?
Write a template loader you can point at a theme directory instead.
I've done something like that, pls take a look here https://github.com/ASKBOT/askbot-devel/blob/master/askbot/skins/loaders.py
Besides the template loader you may need means to resolve media specific to your theme. It can be for example a template tag or a filter that takes some base url and adds theme prefix or something like that, also you could make that automatically keep track of media versions. That way that when you refresh .js or other file the client will have to load the latest version.
not sure if it's the same issue as my question theme switching, template and css file layout on a django site
subdirs of templates can be set as context var which is used as parameter for extend template tag