django templates - include another block from same folder - django

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.

Related

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?.

Including template that is located outside of the main folder

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' %}.

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

Django include template url

I have a template problem.
I have localizated form so, for example, in all italians pages I have the same form and I'm trying to include that instead repeat the same code over and over in the pages.
In
/category1/IT/template.html
I have to include the
/form/it.html
If I use
{% include "form/it.html" %}
the block defined in the it.html is like empty and I see the default [english] form instead the it one.
If I try to write
{% include "../../form/it.html" %}
django tells me that the template does not exixt..
If I cut the it.html code in the IT/template.html this work.
Can someone help me to figure why the blocks in the included file do not work?

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