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?
Related
I have a Django template file working with a passed value like the below.
{% include 'boutique/rating.html' with score=[I want to put value here] %}
When I usually put value into the template, I could easily do it by doing like the below.
{% for store in stores %}
{% include 'boutique/rating.html' with score=store.review_score %}
{% endfor %}
However, as I get into more complex templates, I need to assign the value to the score parameter in the include section through jQuery. Is there a way that I can acheive this through jQuery?
What you need to understand is this: The part of the template between {% and %} is interpreted by Django. It is processed entirely on the server. In other words, it never shows up in the browser, but gets replaced by some sort of standard HTML.
On the other hand, jQuery is a Javascript library and operates entirely in the browser -- it doesn't know anything about the server or Django.
So, to modify the included template with jQuery, you have to find out what HTML it renders to. You can probably do that by looking at the included template file. Then, treat that HTML the way you would any other part of the page for manipulation with jQuery.
I have a bunch of code that I will need to use repeatedly on a page, and on multiple pages. For example, here is a shorter version of the code:
<a href="#"
data-toggle="popover"
title="{% for terms in s_terms %}{% if terms.slug == 'neuron' %}{{terms.title}}{% endif %}{% endfor %}"
data-content="{% for terms in s_terms %}{% if terms.slug == 'neuron' %}{{terms.para_one}}{% endif %}{% endfor %}">
Toggle popover
</a>
There is a lot more code in the block. Now, for obvious reasons I do not want to keep repeating such large chunks of code. I am a fan of the DRY approach.
However, I can't figure out how to render this same piece of code repeatedly. The only thing that would change is the word = "neuron" in there. I thought of using template tags, but that didn't work.
I tried saving the code as a separate file, and inherit it within my template, but then I can't change the keyword ('neuron'). I also tried creating a separate dynamic page, and include that in my Django template, but looks like the include tag only works for templates, and not for dynamic pages.
Can anyone help, please? Thank you, in advance.
You could use Django template built-in template tag include.
From the documentation:
Loads a template and renders it with the current context. This is a
way of “including” other templates within a template.
So, you can just extract your snippet in a separate template and then use it with:
{% include "snippet_template.html" %}
Additionally, you can pass a variable to the include template using the with keyword - you would use this to pass your word parameter:
{% include "snippet_template.html" with word="neuron" %}
As #bonidjukic wrote the include statement is what you search.
But include statement inside for-loop could reach one weakness of Django template Engine (vs Jinja). You include just variables, so it will be fast.
In the case of needing tags (like trans), Django will load tags at each include. Where Jinja will have global "tags".
So just be careful, with how you DRY you templates.
I'm new to Clojure. My issue is regarding selmer templating library.
What I want to do is apply a variable for following way. But its not working;
{% include {{right-page}} %} ------- X
(here right-page is a concatenated sting for a target file name)
Any targets for including in selmer must currently be known when selmer compiles the template, which it does without the passed in data. This means that, given the template:
template.html_template:
{% include right-page %}
and the clojure code:
(selmer/render-file template.html_template
{right-page "some/other/template/file.html_template})
You can expect an exception. As for a work around, you might consider
(selmer/render-file template.html_template
{right-page (selmer/render-file
"some/other/template/file.html_template {})})
or something similar.
You will, of course have to update template.html to include the already rendered text, and to disable escaping, like so:
template.html_template:
{{right-page|safe}}
If right-page is a valid string you're passing to the page, I think this will work for you
{% include right-page %}
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
I've ran into very strange behavior of Django template system. I have a template file, namely test.html, which recursively includes itself:
{% include "test.html" %}
Of course, such template has no chance to be rendered, since there is no finishing condition. OK, let's try the following:
{% if test_false %}{% include "test.html" %}{% endif %},
where test_false is a variable passed to template and equal to False.
One expects that it just will not include anything, but it does:
RuntimeError at /test/
maximum recursion depth exceeded while calling a Python object
I don't get it. Include tag can take arguments from current context, so I doubt it is executed before any other part of the page. Then why does it ignore condition tag?
Django has optimization that include templates that are given by constants at compilation.
Set name of template to variable and include it in that way:
{% include test_template %}
Django will not be able to use it's optimization and your code should work.
Like Thomasz says, Django can only make this optimization if the path is defined as a constant string in the including template - like so:
{% include "test.html" %}
But I would rather not have to put the template path in the context from Python code.
So here is a slightly more self contained way of achieving the same result - wrap the include in a with:
{% with "test.html" as path %}
{% include path %}
{% endwith %}