I want to define a variable if a condition is met:
{% if order_item.status.ordering >= 60 and is_client %}
{% with readonly=1 %}
{% else %}
{% with readonly=0 %}
{% endif %}
...some code
{% endwith %}
However, I get the following error:
Invalid block tag: 'else', expected 'endwith'
How can I fix this bug in django?
Perhaps it's better to add a readonly property/method to your order_item.status.
Alternatively you could also try a templatefilter or templatetag.
This code doesn't make any sense, first you should learn the with syntax. You have opened it twice and closed once and you don't have any useful code inside with.
Related
In my table and in the some record I have lot of link_img, but I want only the first link_img, what can I do ?
I have something like this in my temp
{% for link in sousimg %}
{% if article.article_img.id == link.img_link.id %}
{{ link.link_img }}
{% endif %}
{% endfor %}
This type of question is already present so I suggest having a look at it.
django for loop counter break
You have to do a little bit of hit and trial in this case.
I'm trying to do a check on my Django template to see if the 'date_added' field for a particular post is within the last three days.
I'm trying something like this:
{% for deal in deals %}
{% if deal.date_added > (timezone.now ()- timezone.timedelta(days=4)) %}
<h1>HOT</h1>
{% else %}
do some other stuff
{% endif %}
{% endfor %}
I'm getting this error: Could not parse the remainder: '(timezone.now' from '(timezone.now'
so something tells me deep inside that this is absolutely not possible to have this sort of conditional statement run from within the template---but figured i'd check. thanks.
class Deal(models.Model):
...
def get_label(self):
if self.date > datetime.date.today() - datetime.timedelta(days=4):
return "<h1>HOT</h1>"
else:
return "OLD..."
... then in your template
{% for deal in deals %}
{{ deal.get_label | safe }}
{% endfor %}
I would like to create new variable in django template, which will have a value of comparison
obj.site.profile.default_role == obj
Unfortunately none of this code works:
{% with obj.site.profile.default_role==obj as default %}{% endwith %}
{% with default=obj.site.profile.default_role==obj %}{% endwith %}
What is the proper syntax?
with can take just a "plain" context variable.
You could try assignment-tags instead, passing your params to it.
#register.assignment_tag
def boolean_tag(default_role, obj):
return default_role == obj
and in the template
{% boolean_tag obj.site.profile.default_role obj as my_check %}
This solution is good if variable is used in one template block (like the case of yours, when you try using with). If you need variable in several page blocks, adding it to page context with include tag is better
with tag have no support for value evaluation.
The only possible template-only solution that I can imaging is to split part of html to sub-template and use {% include %} tag
{% if obj.site.profile.default_role==obj %}
{% include 'subtemplate.html' with default=True %}
{% else %}
{% include 'subtemplate.html' with default=False %}
{% endif %}
I'm building a website using Flask with its jinja2 templating engine and I'm dynamically building the menu (as described here):
{%
set navigation_bar = [
('/', 'index', 'Home'),
('/aboutus/', 'aboutus', 'About Us'),
('/faq/', 'faq', 'FAQ')
]
%}
{% set active_page = active_page|default('index') -%}
<ul>
{% for href, id, title in navigation_bar %}
<li{% if id == active_page %} class="active"{% endif %}>
{{ title|e }}
</li>
{% endfor %}
</ul>
Now if a user is logged in I want to show some additional things. So at runtime I want to add items to the navigation_bar variable. I tried something like this:
{% if g.user.is_authenticated() %}
{% navigation_bar.append(('/someotherpage', 'someotherpage', 'SomeOtherPage')) -%}
{% endif %}
But unfortunately this results in the following error: TemplateSyntaxError: Encountered unknown tag 'navigation_bar'. Jinja was looking for the following tags: 'endblock'. The innermost block that needs to be closed is 'block'.
So: does anybody know how I can add additional items to a jinja2 variable at runtime? All tips are welcome!
[bonus question]
I was also wondering, what does the - do at the end of {% set active_page = active_page|default('index') -%}?
The error occurs because Jinja can't identify block. Each Jinja block should start from block name. do block from do extension meets your needs. To use it you should add
do extension to jinja extensions. You can do this like so:
app.jinja_env.add_extension('jinja2.ext.do')
And then you can use do extension. Your example should looks like this:
{% if g.user.is_authenticated() %}
{% do navigation_bar.append(('/someotherpage', 'someotherpage', 'SomeOtherPage')) %}
{% endif %}
Here's another simple example.
You will find answer to your bonus question here. In short - removes whitespaces from start or end of block (this depends on where it is located).
To complete the answer of Slava Bacherikov, if you don't have the Jinja "do extension", you can use the tag set:
{% if g.user.is_authenticated() %}
{# use a dummy variable name, we juste need the side-effect of method call #}
{% set _z = navigation_bar.append(('/someotherpage', 'someotherpage', 'SomeOtherPage')) %}
{% endif %}
I want to pass a value to an include tag that is the OPPOSITE of a variable passed in.
This is what I tried (basically):
{% with s_options as not disp %}
{% include "e.html" with show_options=s_options only %}
{% endwith %}
Is there any way to do what I want?
Not sure if this is the best solution, but I just made a new filter:
from django import template
register = template.Library()
#register.filter(name="not_value")
def not_value(true_value):
return not true_value
And then did:
{% load not_value %}
{% with s_options=disp|not_value %} {# WILL NOT WORK WITH "as" #}
{% include "e.html" with show_options=s_options only %}
{% endwith %}
Note that, possibly, this might work as well (though I have not tried):
{% include "e.html" with show_options=s_options|not_value only %}
This is a bit of a hack using the built in filter yesno, but it works:
{% with reversed_value=original_value|yesno:',True' %}
{# Do something #}
{% endwith %}
Note the comma before 'True'. The way it works is that yesno will return an empty string if the original value is True, which will evaluate as False if you're doing any boolean operations on it.