Here is my template path
webapp/
|__templates/
|__frontpage/
| |__home.html
|__home_base.html
In home.html, it has:
{% extends "home_base.html" %}
This file structure will work. However, I want to put home_base.html inside frontpage/, as this makes more sense. However, Django will report home_base.html TEMPLATE DOES NOT EXIST, if home_base.html is moved to frontpage/.
The error says it cannot find the home_base.html file under templates/ folder. Since the home_base.html is moved to frontpage/, why doesn't it search for home_base.html inside frontpage/ first? Any configurations I am missing?
You need to do the following for template to be extended.
{% extends "frontpage/home_base.html" %}
Django does not have an idea where you have have moved your template. It will look for the template according to the templates path you have defined in your settings.
The template loader will look for template in the directories defined in the DIRS setting in the TEMPLATES settings.
From the DIRS setting documentation:
Directories where the engine should look for template source files, in
search order.
Also, if frontpage was an app and you had placed your template in the frontpage app instead of the templates folder, then you can set the APP_DIRS settings to be True. This will tell Django to find the templates in the individual apps also.
From the APP_DIRS setting documentation:
Whether the engine should look for template source files inside
installed applications.
Related
I am picking up some old Django code and am puzzled by a line in the site's master template.
After a series of {% load xxx %} lines there is the line
{% request_source 849051 %}
I have been unsuccessful in finding any documentation for this command. Is it a standard template command? Or is it something custom to this code, and if so where would I likely find the implementation?
The site was written for Django 1.5.12 if that makes a difference.
{%load ... %} loads various python routines from the templatetags directory of an installed django app (in installed_apps in settings.py) think of this like import package but you are limitted to packages only available inside the templatetag folder of an installed app
{% funciton_call arg1 arg2 ... %} is calling a function defined in the python files in the templatetags directory
request_source is not a django thing, it is a function in one of the templatetag files
I am playing with django-gentelella and try to add custom template tags to the project.
According to the latest Django documentation, one should add a "templatetags" directory, at the same level as models.py, views.py, etc. Also, an init.py file should be placed in the directory.
I added my template tags into a file called "template_tags.py" and restarted the server. In my templates, I load the file using "{% load template_tags %}" at the top of the file.
Unfortunately, this does not work yet. According to the Django documentation, it is also required to add the template_filters to the INSTALLED APPS.
My problem is that I cannot figure out how to get the right path in dot notation. Can anyone point me in the right direction?
/profiles/templatetags/custom_tags.py
#register.filter(name='getLocalTimeDifference')
def getLocalTimeDifference(value):
value = value.replace(..)
return value
/profiles/templates/navbar.html
{% load custom_tags %}
...
<div class="notification-meta">
<small class="timestamp">{{ notification.timesince | getLocalTimeDifference}} before </small>
</div>
P.S. __init_.py should also be added to templatetags directory
I'm having trouble including within a template thats it. I cannot find in the documentation where the include goes to look for the included .html (I assumed it would be the same as the templates?), I'm putting {% include "xy.html" %} in one file, but it looks like xy.html is not being found. I can't find any error returned either. xy.html is in the same directory as its calling file which is in a template folder in Django parlance.
I get no error message displayed, I simply dont get the included file displayed.
Where should I place the xy.html file?
According to the include documentation:
The template name can either be a variable or a hard-coded (quoted)
string, in either single or double quotes.
Put the template name into quotes:
{% include "xy.html" %}
FYI, xy.html should be anywhere inside TEMPLATE_DIRS directories.
FYI2, setting TEMPLATE_DEBUG setting to True would help to see the detailed error message in case of an exception raised while rendering the template.
Let's use an example project called foo:
foo
-blog
-admin.py
-models.py
-views.py
-tests.py
-templates
-blog
-post.html
-news
-admin.py
-models.py
-views.py
-tests.py
-templates
-newsitem.html
-foo
-settings.py
-urls.py
-wsgi.py
-templates
-base.html
-blog
-post.html
If your settings.py includes:
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates')
)
Then Django overrides app templates with the TEMPLATE_DIRS template, this means that if a post.html resides both in blog/templates/blog/post.html and templates/blog/post.html (as the above example)
then Django will load the later.
If you don't specify a TEMPLATE_DIRS then Django searches for the post.html within the templates folder of each app, this means that if you specify a: blog/templates/blog/post.html and a news/templates/blog/post.html both are valid,
in this occasion Django will load the template depending on how you INSTALLED_APPS looks like:
INSTALLED_APPS = (
...
'blog',
'news',
...
)
Then Django will render the template of the blog app instead of the template of news app (if we had a blog/post.html under the templates of news).
Yet if your INSTALLED_APPS looks like:
INSTALLED_APPS = (
...
'news',
'blog',
...
)
Then Django will load post.html from the news app rather than blog app (again if we had templates/blog/post.html under our news app).
You should also be picky about template names, templates with similar names will override each other depending on your settings and INSTALLED_APPS order (higher overrides lower), TEMPLATE_DIRS templates always override all others.
Below is the structure of my Django App:
Project/
static/
App1/
App2/
App3/
...
So I have a Django project, and I want to keep the HTML/CSS styling uniform across the apps in the project.
How does one go about extending a HTML template residing in the Project folder from each of these apps?
I have only been successful extending HTML templates within App folders for each respective App.
My project settings has static files set to be located in "/static/"
You can put your project-common templates in Project/templates and add the /path/to/Project/templates to your TEMPLATE_DIRS setting in your settings.py file. prepend the root to everything else, so it gets searched first:
TEMPLATE_DIRS = (
# Don't forget to use absolute paths, not relative paths.
"/path/to/Projects/templates",
#other dirs ...
)
Then you go extend it from your application templates as usual:
{# App1/templates/App1/template1.html #}
{% extends "template_in_project_root.html" %}
...
the TEMPLATE_DIRS setting
I'm trying to combine Django with Jade, but I've had some problems.
I have model which is named About. This has a view like this:
def about(request):
return render_to_response('about.jade',{},RequestContext(request))
and in my urls I have:
url(r'about/', views.about),
But it provides an error that the Templates doesn't exist (and yes, it exists). Is it correct to write the url like this?
Any help would be appreciated!
If your getting the big Template does not exist page in your browser, this usually means that django cannot find where you have stored the your template file (irrespective of using jade).
If you ve created a djnago 1.6 project you need to add the following line to settings:
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]
Then create a templates directory inside your app (not project) directory, and put your template there.