Recently I switched my templating engine from default to Jinja2/Coffin. Everything works just fine but I'm having troubles trying to use Django/Jinja2 django-paging (http://linux.softpedia.com/get/Internet/HTTP-WWW-/django-paging-58496.shtml) extension in my project.
There is an example how to use this extension with Jinja:
{% with paginate(request, my_queryset) as results %}
{{ results.paging }}
{% for result in results.objects %}
{{ result }}
{% endfor %}
{{ results.paging }}
{% endwith %}
Simply, I don't know where and how to define this new tag paginate to be recognized by Jinja2 engine.
I tried to put is in settings.py as:
JINJA2_EXTENSIONS = (
'paging.helpers.paginate',
)
but the error is raised:
paginate() takes at least 2 arguments (1 given)
Any help is appreciated.
Ok, problem solved. The paging application should be added into INSTALLED_APPS (settings.py)
Related
In my HTML code i have the following jinja template code:
{% for site in sites %}
{{ site.site_name }}
{% endfor %}
The issue is: let's assume that {{ site.site_link }} = www.domainname.com, so whenever the link is clicked it supposed to direct the user to the domainname page. but, it leads to :
http://127.0.0.1:5000/www.domainname.com
which is a 404..Any idea why is this happening and how to fix it?
I don't know if this is relevant, but, I am using flask Blueprints.
You need to put http:// in front of it, like href="http://........"
{% for site in sites %}
{{ site.site_name }}
{% endfor %}
i am a new to django, and i'm trying to execute a django code in an html template, but it is showing the same error and i dont know how to make it work. I am trying to read some files and find the one that contains the string. This is the code in the template:
{% if documents %}
<ul>
{% for document in documents %}
{% if 'TheString' in open('document.docfile.url').read() %}
<li>{{ document.docfile.name }}</li>
{% else %}
<li></li>
{% endif %}
{% endfor %}
</ul>
{% else %}
<p>No documents available.</p>
{% endif %}
The error is the following:
TemplateSyntaxError at /subir/
Could not parse the remainder: '('document.docfile.url').read()' from 'open('document.docfile.url').read()'
What could it be, or am i doing it wrong? Thanks in advanced.
Your problem is you are trying to use Python code in the template, like your open() call.
Django templates don't work like that - you can only use a very limited set of syntax, and the syntax is not the same as Python.
Everything inside {{ }} and {% %} needs to be special Django template syntax, not Python code.
See the Django Template language documentation for the full details.
You can't use code like this in a template directly see the documentation for available template tags and filters. From what I can tell by the code you have posted in your view.py you'll need something like this:
docs=[]
for document in documents:
if 'TheString' in open('/path/to/document').read():
docs.append(document)
after passing docs to the template you could do this in the template:
{% for doc in docs %}
<li>{{ document.docfile.name }}</li>
{% empty %}
<p>No documents available.</p>
{% endfor %}
I'd need to look at what you are passing from the view to be sure of this, but I'd gladly assist in further help with further information if this doesn't help.
I have almost literally exactly what is in the django docs
{% if things|length > 1 %}
<div>
<span>Multiple things were found for this search criteria</span>
<button>Show All</button>
</div>
{% endif %}
And yet the div is showing up when things|length is 0.
I'm using Django 1.3.1, so I don't know if that's the problem-- I couldn't find that information. It wasn't in the 1.4 release notes, at least.
What the heck is going on? Is this a 1.3 problem, or...?
Edit: really looks like this was introduced in 1.2 and should be working. So what's the deal?
Try
{% if itineraries.items|length > 1 %}
...
{% endif %} or only {% if itineraries %}
Using Django templating engine I can include another partial template while setting a custom context using named arguments, like this:
{% include "list.html" with articles=articles_list1 only %}
{% include "list.html" with articles=articles_list2 only %}
As you may be supposing, articles_list1 and articles_list2 are two different lists, but I can reuse the very same list.html template which will be using the articles variable.
I'm trying to achieve the same thing using Jinja2, but I can't see what's the recommended way, as the with keyword is not supported.
Jinja2 has an extension that enables the with keyword - it won't give you the same syntax as Django, and it may not work the way you anticipate but you could do this:
{% with articles=articles_list1 %}
{% include "list.html" %}
{% endwith %}
{% with articles=articles_list2 %}
{% include "list.html" %}
{% endwith %}
However, if list.html is basically just functioning as a way to create a list then you might want to change it to a macro instead - this will give you much more flexibility.
{% macro build_list(articles) %}
<ul>
{% for art in articles %}
<li>{{art}}</li>
{% endfor %}
</ul>
{% endmacro %}
{# And you call it thusly #}
{{ build_list(articles_list1) }}
{{ build_list(articles_list2) }}
To use this macro from another template, import it:
{% from "build_list_macro_def.html" import build_list %}
This way you can pass multiple variables to Jinja2 Include statement - (by splitting variables by comma inside With statement):
{% with var_1=123, var_2="value 2", var_3=500 %}
{% include "your_template.html" %}
{% endwith %}
For readers in 2017+, Jinja as of 2.9 includes the with statement by default. No extension necessary.
http://jinja.pocoo.org/docs/2.9/templates/#with-statement
In older versions of Jinja (before 2.9) it was required to enable this feature with an extension. It’s now enabled by default.
Updated 2021+
Included templates have access to the variables of the active context by default. For more details about context behavior of imports and includes, see Import Context Behavior.
From Jinja 2.2 onwards, you can mark an include with ignore missing; in which case Jinja will ignore the statement if the template to be included does not exist. When combined with with or without context, it must be placed before the context visibility statement. Here are some valid examples:
{% include "sidebar.html" ignore missing %}
{% include "sidebar.html" ignore missing with context %}
{% include "sidebar.html" ignore missing without context %}
Another option, without plugins, is to use macros and include them from another file:
file macro.j2
{% macro my_macro(param) %}
{{ param }}
{% endmacro %}
file main.j2
{% from 'macro.j2' import my_macro %}
{{ my_macro(param) }}
I've noticed that Django 1.3 introduced the new "localize" tag/filter.
http://docs.djangoproject.com/en/1.3/topics/i18n/localization/
It says:
To activate or deactivate localization
for a template block, use:
{% localize on %}
{{ value }}
{% endlocalize %}
{% localize off %}
{{ value }}
{% endlocalize %}
However I always got the error message:
Invalid block tag: 'localize'
Looks like the "localize" filter doesn't work as well. Can anybody help me? Or can anybody else confirm if this is a bug? Thanks!
Have you loaded the localization template tags with:
{% load l10n %}
before using "localize" template tag?