I'm getting a strange error. Putting {% if any([None, 1]) %}show{% endif %} in my Django template creates a TemplateSyntaxError.
Experimentation shows that {% if True %}show{% endif %} works so I know the problem is from any([None, 1]). I checked that any([None, 1]) in python outside of Django and it returned True as expected so my code seems like it should work in the template. What am I doing wrong?
Thank you for you time.
I think you should use proper boolean operators with {% if %} Django built-in:
https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#boolean-operators
Or write your own templatag as suggested by Nafees Anwar
Related
The error is: Invalid block tag: 'endif'.
First about what the error is about - i wrapped whole template into condition:
{% if something %}
{% else %}
{% load cms_tags sekizai_tags menu_tags %}
{% render_block "css" %}
{% render_block "js" %}
{% endif %}
And this causes django to throw error:
TemplateSyntaxError at /mypage/
Invalid block tag: 'endif'
But the problem is clearly with classytags/sekizai as
{% if something %}
{% else %}
<h1>Hello world</h1>
{% endif %}
renders without errors.
Does anyone know how i could avoid this error or fix it?
Now second part - what i am trying to achieve is - i want to display somewhat different HTML page when request is ajax request. The condition should be if request.is_ajax. But if changed to to illustrate my point in clearer way. I am using django-cms and it allows you to only set one template per each page. And this would not be a problem if i could serve same content but with different surrounding html in case of ajax requests, but unfortunately the rendering fails...
Well, it is pretty clear in the documentation that using {% render _block ... %} inside another template tag block is not allowed.
Couldn't you just do that if condition inside the rendered block?
edit: Just noticed the question was not so recent. Oh well. Curious about how you ended up solving your problem...
I'm trying to run this code in my django template:
{% if lawChoice.releveAnnee==law.releveAnnee %}
I get the following error:
Could not parse the remainder: '==law.releveAnnee' from 'lawChoice.releveAnnee==law.releveAnnee'
I think law.releveAnnee is considered as an object, not as an integer, that's why the comparison fails!
Thanks in advance for your help,
Romain
EDIT
Thanks to the answer above: a SPACE is needed BEFORE and AFTER the ==:
{% if lawChoice.releveAnnee == law.releveAnnee %}
This works! Solved :)
Probably that's because you need to add a space before and after the == operator.
You'd better go for ifequal
{% ifequal lawChoice.releveAnnee law.releveAnnee %}
...
{% endifequal %}
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 %}
Is it possible to create a Django template tag which evaluates to a boolean?
Eg, can I do:
{% if my_custom_tag %}
..
{% else %}
..
{% endif %}
At the moment I've written it as an as tag, which works fine like this:
{% my_custom_tag as var_storing_result %}
But I was just curious if I could do it the other way as I think it'd be nicer if I didn't have to assign the result to a variable first.
Thanks!
Actually.. what you can do is register tag as assignment_tag instead of simple_tag Then in your template you can just do {% my_custom_tag as var_storing_result %} one time and then regular if blocks where ever you want to evaluate the boolean. Super useful! For example
Template Tag
def my_custom_boolean_filter:
return True
register.assignment_tag(my_custom_boolean_filter)
Template
{% my_custom_boolean_filter as my_custom_boolean_filter %}
{% if my_custom_boolean_filter %}
<p>Everything is awesome!</p>
{% endif %}
Assignment Tag Official Doc
One alternative might be to define a custom filter that returns a boolean:
{% if my_variable|my_custom_boolean_filter %}
but that will only work if your tag depends on some other template variable.
You'd have to write a custom {% if %} tag of some sort to handle that. In my opinion, it's best to use what you already have in place. It works well, and is easy for any other developers to figure out what's going on.
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)