Including template that is located outside of the main folder - django

In my main template folder I have 2 folders
explorer and includes
template
explorer
body.html
includes
header.html
I want to include in my body.html file header.html from includes folder
tried those options :
{% include "../includes/header.html" with active_tab='dashboard' %}
{% include "/includes/header.html" with active_tab='dashboard' %}
in both options - can not find the path
What is the correct way of doing it?

Django template paths are always absolute, from the root of the template folder (or whatever folder is included in the TEMPLATE dirs setting.) So it's just {% include 'includes/header.html' %}.

Related

TemplateSyntaxError: Templates and static files

I have components for the website and they all work fine, for example I have a navbar.html and works flawlessly. However, I think the issue begins when I have a static file. I have another component called 'header.html' (which contains a profile picture) and SHOULD have loaded up a header in my index.html file. But it spits 'TemplateSyntaxError' and says that the issue is with the line code '{% static ... %}'.
My settings.py seems correct (see below). I also included 'django.contrib.staticfiles' and {% load static %} in the main index.html.
This is also the html part:
The structure of my project is as such:
-myproject
--base
--myproject
--static
--staticfiles
Did you load static on the template?
Add {% load static %} to the top of your html file

Where to put global html/css files in Django (Navbar.html, general.css, etc...) so that it's referable by all apps?

1) I want my project to be able to call {% include navbar.html %} from every template but I'm not sure where I'm supposed to place navbar.html. I tried placing it in mysite/templates/navbar.html but calling {% include navbar.html %} just gave me a TemplateSyntaxError.
2) I also want to be able to have a general.css referable by all of my apps. Is placing it in mysite/static/general.css a good idea?.

using "static" template tag from "included" template?

I'm getting an Invalid block tag: 'static' error when using a {% static .. %} tag from a template which is {% include %}ed by another template.
The later template has {% load staticfiles %}.
Do I still need to load it again from the included template?
Yes you do need to load it again. If you look at the docs for the include template tag you will notice:
The include tag should be considered as an implementation of “render this subtemplate and include the HTML”, not as “parse this subtemplate and include its contents as if it were part of the parent”. This means that there is no shared state between included templates – each include is a completely independent rendering process.
Note that the template you are loading uses the context of the parent template, so that's why you still have access to all the variables of the parent tepmlate

Why does using a variable in settings.ALLOWED_INCLUDE_ROOTS won't let me use {% ssi %}?

Problem
I'm using Django 1.3. I will have to use many different JavaScript functions (like 10 or something) in my template.
What I first did was to put them in the <script> tag, which worked fine. But now that it works, I want to separate them from the template code. It would make the code way more read'able.
So I thought of using the {% ssi "..." parsed %} thing. Since I use Django template tags in my JavaScript code, I can't just link them from my static files with <script src="..."></script>.
Here is what works :
# This will allow the {% ssi %} tag to include files from the given paths
ALLOWED_INCLUDE_ROOTS = (
'/THIS/IS/THE/FULL/PATH/TO/MY/PROJECT/static/js/',
)
Here is what does not work :
# Project root for further paths
PROJECT_PATH = os.path.dirname(__file__)
# This will allow the {% ssi %} tag to include files from the given paths
ALLOWED_INCLUDE_ROOTS = (
PROJECT_PATH+'/static/js/',
)
I double-checked that the two strings were the same (with ./manage shell) and they are exactly the same (with trailing / and all).
Questions
Why does the second code renders me [Didn't have permission to include file] in my template ?
Also, how should I link the file to include in the {% ssi %} tag ? Since {% get_static_prefix %} does not work, I'm currently using the file's full path, which is ugly.
As odd as it may appear, I didn't make any change in my settings.py but it is now functionnal. I believe Mike Cooper was right and some remote code was breaking ALLOWED_INCLUDE_ROOTS path.
ALLOWED_INCLUDE_ROOTS is likely a constant due to it's naming convention. Constants aren't meant to be variable.
http://en.wikipedia.org/wiki/Constant_(programming)#Naming_conventions

django templates - include another block from same folder

I'm new to django, I'm trying to figure out how to include other html files from the same subfolder to my main template file.
I've tried the following to no avail:
{% include './_shared.html' %}
I'll eventually have many template folders. Can someone help me out?
You have to put your sub folders inside your template directory
You have to define your template path in your settings.py
now you can include that template file like {% include '/yoursubfolder/file.html' %}
Try this:
{% include request.path+"_shared.html" %}
You might have to add slash between path and file name.