I'm using the following template code to try to produce a list in the format 'a, b, c and d':
{% for item in list %}
{% if not forloop.first %}
{% if not forloop.last %}
,
{% endif %}
{% endif %}
{% if forloop.last %}
and
{% endif %}
{{ item }}
{% endfor %}
The actual output I'm getting is 'a , b , c and d' (note the spaces before the commas).
What's going on and how do I fix it?
OK, after several attempts I think I've found a solution, it's a bit verbose but it does what you want - no extra spaces -
{% for item in list %}
{% if forloop.revcounter > 2 %}
{{ item }},
{% else %}
{% if forloop.revcounter == 2 %}
{{ item }} and
{% else %}
{{ item }}
{% endif %}
{% endif %}
{% endfor %}
forloop.revcounter is counting down from the end of the loop, so you add the 'and' to the second last item and leave the last one without anything.
Django inserts all the spaces that your template contains:
{% for item in list %}{% if not forloop.first %}{% if not forloop.last %}, {% endif %}{% endif %}{% if forloop.last %} and {% endif %}{{ item }}{% endfor %}
By the way, your template renders the wrong output if the list contains only one value. The corrected template looks like:
{% for item in list %}
{% if not forloop.first %}
{% if forloop.last %}
and
{% else %}
,
{% endif %}
{% endif %}
{{ item }}
{% endfor %}
And without the unwanted space it becomes:
{% for item in list %}{% if not forloop.first %}{% if forloop.last %} and {% else %}, {% endif %}{% endif %}{{ item }}{% endfor %}
I would make a simple template filter:
#register.filter
def format_list(li):
"""
Return the list as a string in human readable format.
>>> format_list([''])
''
>>> format_list(['a'])
'a'
>>> format_list(['a', 'b'])
'a and b'
>>> format_list(['a', 'b', 'c'])
'a, b and c'
>>> format_list(['a', 'b', 'c', 'd'])
'a, b, c and d'
"""
if not li:
return ''
elif len(li) == 1:
return li[0]
return '%s and %s' % (', '.join(li[:-1]), li[-1])
I'm far from an expert in python and there's probably a better way to do it. But still, this seems clean at the "django level", use it as such:
{{ your_list|format_list }}
What I like with this solution is that it is reusable, is more readable, has less code, and has tests.
Check out the documentation on writing template filters for details on how to install it.
Also, you might notice that this function comes with doctests, refer to django documentation to learn how to run the tests.
Here's a way:
>>> python -m doctest form.py -v
Trying:
format_list([''])
Expecting:
''
ok
Trying:
format_list(['a'])
Expecting:
'a'
ok
Trying:
format_list(['a', 'b'])
Expecting:
'a and b'
ok
Trying:
format_list(['a', 'b', 'c'])
Expecting:
'a, b and c'
ok
Trying:
format_list(['a', 'b', 'c', 'd'])
Expecting:
'a, b, c and d'
ok
1 items had no tests:
form
1 items passed all tests:
5 tests in form.format_list
5 tests in 2 items.
5 passed and 0 failed.
Test passed.
Related
{% for each_item in item.artifacts %}
{% if each_item.scanner_count > 0 and each_item.scanner_match > 0 %}
{% if forloop.counter <= 5 %}
<tr>
<td>{{each_item.threat_name}}</td>
</tr>
{% else %}
{% if forloop.last %}
<p><b><i> {{ forloop.counter|add:"-5" }} rows were truncated. See full report for more details. </i></b></p>
{% endif %}
{% endif %}
{% else forloop.counter -=1 %}
{% endif %}
{% endfor %}
ERROR:Malformed template tag at line 171: "else forloop.counter -=1"
I want to increment the counter only when if condition is successful. Dont know how to do it with forloop.counter. Goal is to print 5 rows of valid output(scanner count >0 and Scanner match >0)
You can use combination of add and forloop counter to achieve what you want to achieve. But remember you need to pass some variable (I've used rank here for kinda storing the increment variable in template.
views.py
rank = 0
return render(request, "base.html", {"items": items, "rank": rank})
html
{% for each_item in items %}
{% if each_item.scanner_count > 0 and each_item.scanner_match > 0 %}
{% if forloop.counter|add:rank <= 5 %}
<tr><td>{{each_item.threat_name}}</td></tr>
<br>
{% else %}
{% if forloop.last %}
<p><b><i> {{ forloop.counter|add:"-5" }} rows were truncated. See full report for more details. </i></b></p>
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
As other commenters have noted, you cannot make an assignment of the forloop.counter in the template, as that would be considered logic that should be in the view (controller). Remove the {% else forloop.counter -= 1 %} from your code and it should work as I think you intended. If not, either add logic in the object (context) being passed to the template or user other forloop attributes/variables as found in the Django documentation here: https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#for
I have a jinja expression as:
{% if meaning['example'] %}
{{ meaning['example'] }}<br>
{% endif %}
Which gives me o/p:
you don't know jack
Here word['word'] has the value jack in the o/p
How can I make the jack bold in the expression?
WRT SO I have tried as:
{{ <strong>word['words']</strong> if word['words'] in meaning['example'] }}
But this didn't work , Any help is much appreciated , TIA
{% if meaning['example'] %}
{% for w in meaning['example'] %}
{% if w == word['word']%}
<strong>{{w}} </strong>
{% else %}
{{w}}
{% endfor %}
{% endif %}
if you have number of word then replace
{% if w == word['word']%} -->> {% if w in word %}
You have a couple of options:
in an if block
{% if word['words'] in meaning['example'] %}
<strong>word['words']</strong> }}
{% endif %}
in a ternary if expression
{{ '<strong>' + word['words'] + '</strong>' if word['words'] in meaning['example'] else '' }}
I personally prefer the first option if I want to write markup, or the conditional expression is relatively long as in your example.
For anyone's ref
Thanks for #Mayank Jain comment from where i digged this solution for my Q :
{% if meaning['example'] %}
{% for w in meaning['example'].split(' ') %}
{% if w == word['word']%}
<strong>{{w}} </strong>
{% else %}
{{w}}
{% endif %}
{% endfor %}
{% endif %}
I have strings like the following:
Team Li vs. Team Aguilar || SCORE (W-L-T): 5-4-0
Favors Flavors vs. Cupcakes For Breakfast || SCORE (W-L-T): 11-2-1
I would like the text to be green if the "W" value is greater than the "L" value, and red if the "L" value is greater than the "W" value. I have the following code in Jinja2 that works for the first case, but does not work for the second case. It incorrectly displays the string as red, even though the "W" column is greater than the L column.
{% for item in matchups %}
{% if (item[-5]|int) > (item[-3]|int) %}
<font color='green'>{{ item }}</font>
{% elif (item[-5]|int) == (item[-3]|int) %}
{{ item }}
{% else %}
<font color='red'>{{ item }}</font>
{% endif %}
<br>
{% endfor %}
I understand that my code fails because the second string has 2 digits. Is there a good way to fix this issue?
This is a Jinja2 problem, so answers in Jinja2 would be great. However, a Python solution might also work too.
You can extract the elements with two splits (using variables for clarity):
first to get the last column (split by whitespace) element:
{% set results = item.split()[-1] %}
then to get the first and second of the results (split by the dash):
{% set w = results.split('-')[0]|int %}
{% set l = results.split('-')[1]|int %}
The full code (also with a condition to process only lines containing SCORE to handle the one from your now-edited-out example with *************):
{% for item in matchups %}
{% if 'SCORE' in item %}
{% set results = item.split()[-1] %}
{% set w = results.split('-')[0]|int %}
{% set l = results.split('-')[1]|int %}
{% if w > l %}
<font color='green'>{{ item }}</font>
{% elif w == l %}
{{ item }}
{% else %}
<font color='red'>{{ item }}</font>
{% endif %}
<br>
{% endif %}
{% endfor %}
I've got a Django template I'd sometimes like to pass a list and sometimes like to pass a single value. How can the template tell which it was given?
I'm thinking the value would be set like one of these:
context = {
'foo' : 'bar
}
or:
context = {
'foo' : ['bar', 'bat', 'baz']
}
Then, the template would have code that looks something like this:
{% if foo isa list %}
{% for item in foo %}
{{ item }}<br>
{% endfor %}
{% else %}
{{ item}}<br>
{% endif %}
I can set it up to have foo or foolist, for example, and check for one or the other. However, it'd be a bit nicer (imo) to just have foo that was either a list or not.
If you intend to do it this way then just add a check that it doesn't have format(in case of string) method and has 0 index, if so then its a list else considered single value
{% if foo.0 and not foo.format %}
{% for item in foo %}
{{ item }}<br>
{% endfor %}
{% else %}
{{ item}}<br>
{% endif %}
I think your approach is needlessly complicated.
I would just go with a list:
views.py
foo_list = ['bar']
context = {
'foo': foo_list,
'foo_len': len(foo_list),
}
template
{% if foo_len == 1 %}
{{ foo.0 }}
{% else %}
{% for item in foo %}
{{ item }}
{% endfor %}
{% endif %}
I want to change the value of the variable declared outside the loop within a loop. But always changing, it keeps the initial value outside the loop.
{% set foo = False %}
{% for item in items %}
{% set foo = True %}
{% if foo %} Ok(1)! {% endif %}
{% endfor %}
{% if foo %} Ok(2)! {% endif %}
This renders:
Ok(1)!
So the only (bad) solution have found so far was this:
{% set foo = [] %}
{% for item in items %}
{% if foo.append(True) %} {% endif %}
{% if foo %} Ok(1)! {% endif %}
{% endfor %}
{% if foo %} Ok(2)! {% endif %}
This renders:
Ok(1)!
Ok(2)!
But, its is very ugly! Is there another more elegant solution?
Try also dictionary-based approach. It seems to be less ugly.
{% set vars = {'foo': False} %}
{% for item in items %}
{% if vars.update({'foo': True}) %} {% endif %}
{% if vars.foo %} Ok(1)! {% endif %}
{% endfor %}
{% if vars.foo %} Ok(2)! {% endif %}
This also renders:
Ok(1)!
Ok(2)!
as mentioned in the documentation:
Please note that assignments in loops will be cleared at the end of
the iteration and cannot outlive the loop scope.
but as of version 2.10 you can use namespaces:
{% set ns = namespace(foo=false) %}
{% for item in items %}
{% set ns.foo = True %}
{% if ns.foo %} Ok(1)! {% endif %}
{% endfor %}
{% if ns.foo %} Ok(2)! {% endif %}
You could do this to clean up the template code
{% for item in items %}
{{ set_foo_is_true(local_vars) }}
{% if local_vars.foo %} Ok(1)! {% endif %}
{% endfor %}
{% if local_vars.foo %} Ok(2)! {% endif %}
And in the server code use
items = ['item1', 'item2', 'item3']
#---------------------------------------------
local_vars = { 'foo': False }
def set_foo_is_true(local_vars):
local_vars['foo'] = True
return ''
env.globals['set_foo_is_true'] = set_foo_is_true
#---------------------------------------------
return env.get_template('template.html').render(items=items, local_vars=local_vars)
This could be generalized to the following
{{ set_local_var(local_vars, "foo", False) }}
{% for item in items %}
{{ set_local_var(local_vars, "foo", True) }}
{% if local_vars.foo %} Ok(1)! {% endif %}
{% endfor %}
{% if local_vars.foo %} Ok(2)! {% endif %}
And in the server code use
items = ['item1', 'item2', 'item3']
#---------------------------------------------
local_vars = { 'foo': False }
def set_local_var(local_vars, name, value):
local_vars[name] = value
return ''
env.globals['set_local_var'] = set_local_var
#---------------------------------------------
return env.get_template('template.html').render(items=items, local_vars=local_vars)