Jinja loop.index does not print - python

When running the following jinja code, I only get "Column info" printed. Why the index does not appears ?
{% for field in columns_form %}
{% if 'title_' in field.name %}
<td>Column {{ loop.index }} info</td>
{% endif %}
{% endfor %}

It sounds like the template is being treated as a Django template, not a Jinja template.
Using {{ loop.index }} should work in a Jinja template, but wouldn't work in a Django template, where you would use {{ forloop.counter }} instead.

In case {{ loop.index }} does not work in the latest version, a workaround is to zip the columns_form and range(0, len(columns_form)+1) in the python file as
columns_form_idx = zip(columns_form, range(0, len(columns_form)+1))
In the template file,
{% for field, idx in columns_form_idx %}
{% if 'title_' in field.name %}
<td>Column {{ idx }} info</td>
{% endif %}
{% endfor %}

Related

How to render the DateField on a page in django

Hello I just started learning django and I am having problems rending the content the way I want to. Ideally I want to manage a system of tickets and have the ticket number, start-date, end-date and status appear on the page. Similar to the below format
{% extends 'base.html' %}
{% block content %}
<h1>test</h1>
{% for cr in crs %}
<p>{{ cr }}</p>
{% endfor %}
{% endblock content %}
From what I have in my views file the only thing being displayed when i view it in my web browser is only the ticket number.
Any help would be appreciated.
You should render the attributes .start_date and .end_date in your template as well, so:
{% for cr in crs %}
{{ cr.cr_number }} {{ cf.start_date }} {{ cr.end_date }} {{ cr.get_status_display }}
{% endfor %}
You can specify the date format with the |date template filter [Django-doc]:
{% for cr in crs %}
{{ cr.cr_number }} {{ cf.start_date|date:"Y-m-d" }} {{ cr.end_date|date:"Y-m-d" }} {{ cr.get_status_display }}
{% endfor %}

How to pass template variable to slice filter in Django templates

I'm trying to slice loop in django template with variable
USUAL WAY
{% for article in module.module_article_key.module_article_category.article_category_key.all|slice:":2" %}
{{ article.article_title }}
{% endfor %}
WHAT NEEDS
{% for article in module.module_article_key.module_article_category.article_category_key.all|slice:":module.module_article_key.module_article_count" %}
{{ article.article_title }}
{% endfor %}
so we have working variable {{ module.module_article_key.module_article_count }}
normaly this variable gives integer value stored for this module, however wen i use it to slice loop - nothing happens
You need to cast module_article_count to string first then making articleSlice via nested {% with %} and use the resulting template variable in slice filter as follow:
{% with articleCount=module.module_article_key.module_article_count|stringformat:"s" %}
{% with articleSlice=":"|add:articleCount %}
{% for article in module.module_article_key.module_article_category.article_category_key.all|slice:articleSlice %}
{{ article.article_title }}
{% endfor %}
{% endwith %}
{% endwith %}

Django loop through list

How do you loop trough a list in Django templates with a for loop?
my problem is with trying to fetch list.0 list.1 list.2
But when running the code i don't get anything
{% for i in range %}
{% for key, value in list.i.items %}
{{ key }}
{{ value }}
{% endfor %}
{% endfor %}
where i should be list.0-2 but it doesn't show anything on screen
If it is just a list containing a dict?
Just use the following code
{% for element in lst %}
{% for key, value in lst.items %}
{{ key }} --> {{ value }}
{% endfor %}
{% endfor %}

Loop through a Python list in Django template with for loop

I need to simplify this Django template,
{{ var.1 }}
{{ var.2 }}
{{ var.3 }}
{{ var.4 }}
{{ var.5 }}
var is Python list passed as context to the template
how do you convert the above template using a for tag construct. I tried this but does not work.
{% for i in var|length %}
{{ var.i }}
{% endfor %}
You can just do
{% for x in var %}
{{x}}
{% endfor %}

How to output a comma delimited list in jinja python template?

If I have a list of users say ["Sam", "Bob", "Joe"], I want to do something where I can output in my jinja template file:
{% for user in userlist %}
{{ user }}
{% if !loop.last %}
,
{% endif %}
{% endfor %}
I want to make the output template be:
Sam, Bob, Joe
I tried the above code to check if it was on the last iteration of the loop and if not, then don't insert a comma, but it does not work. How do I do this?
You want your if check to be:
{% if not loop.last %}
,
{% endif %}
Note that you can also shorten the code by using If Expression:
{{ ", " if not loop.last else "" }}
You could also use the builtin join filter like this:
{{ users|join(', ') }}
And using the joiner from https://jinja.palletsprojects.com/templates/#joiner
{% set comma = joiner(",") %}
{% for user in userlist %}
{{ comma() }}{{ user }}
{% endfor %}
It's made for this exact purpose. Normally a join or a check of forloop.last would suffice for a single list, but for multiple groups of things it's useful.
A more complex example on why you would use it.
{% set pipe = joiner("|") %}
{% if categories %} {{ pipe() }}
Categories: {{ categories|join(", ") }}
{% endif %}
{% if author %} {{ pipe() }}
Author: {{ author() }}
{% endif %}
{% if can_edit %} {{ pipe() }}
Edit
{% endif %}

Categories