How to display N number of Backward relationship in Django templates? - django

{% for category in categories %}
{% for product in categories.product_set.all %}
<h1> {{ product.name }} </h1>
{% endfor %}
{% endfor %}
I want to show 10 products instead of all in template

There is a slice filter that you can use in templates:
{% for category in categories %}
{% for product in categories.product_set.all|slice:":10" %}
<h1> {{ product.name }} </h1>
{% endfor %}
{% endfor %}

Related

Grouping dates in Django templates

im trying to group my objects by year only year in datetime field.
{% regroup notificacoes by criados|date:"Y" as criados_list %}
<ul>
{% for x in criados_list %}
<li>
{{ x.grouper }}
<ul>
{% for y in x.list %}
<li>{{ y.slug }} : {{ y.arquivo }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
this was supposed to work, but is not working

Flaks / Jinja nested if statement

I am stuck on a nested if statement, thinking I am approaching it wrong. I have a list of about 100 products that I am rendering on my page. I want to have a nested 'if' statement. Users will have a toolkit, and if the product is in their toolkit, i want to render "Already Uses", else "Add to toolkit?"
{% for product in products|sort(attribute="name") %}
{{ product.name }}
{% for products in toolkit %}
{% if product.name in toolkit %}
<p>Already Uses<p>
{% else %}
<p>Add to toolkit?<p>
{% endif %}
{% endfor %}
more details on: jinja2 check if value exists in list of dictionaries
You are spot on and on the right path. It would help to provide the structure (as an example) of the product object ...
{% for product in products|sort(attribute="name") %}
{{ product.name }}
{% if product.name in toolkit %}
<p>Already Uses<p>
{% else %}
<p>Add to toolkit?<p>
{% endif %}
{% endfor %}
If toolkit is a nested map map:
{% if product.name in toolkit|map(attribute="<whatever has the value equal with product.name>") %}
<p>Already Uses<p>
{% else %}
<p>Add to toolkit?<p>
{% endif %}

Django loop – remove last comma

I have the following loop set up, but need to remove the comma on the last item (it's to replicate a JSON array for cycle2)
{% for product_in_series in series.get_products %}{%spaceless%}
{% with product_in_series.product as product %}
{%if not forloop.first%}
"<img src='{% version product.get_overview 'page_image' %}'>",
{%endif%}
{% endwith %}
{%endspaceless%}{% endfor %}
Cheers,
R
What about this?
{% for product_in_series in series.get_products %}{%spaceless%}
{% with product_in_series.product as product %}
{%if not forloop.first%}
"<img src='{% version product.get_overview 'page_image' %}'>"
{%if not forloop.last%},{%endif%}
{%endif%}
{% endwith %}
{%endspaceless%}{% endfor %}
{{ forloop.last|yesno:",,"|safe }}
, - is a comma
None of the above works for me.
The correct syntax, as in Django 3.0, is as such
{% with querythisandthat as A %}
{% for u in A %} {{ u.interesting_stuff }}
{% if u == A.last %} . {% else %} ; {% endif %}
{% endfor %}
{% endwith %}
The reason is that A.last is not True/False, but it is the last element of the queryset
https://docs.djangoproject.com/en/3.0/ref/models/querysets/#django.db.models.query.QuerySet.first

Optimizing a django template full of {% regroup %} tags

I have a Django template full of nested {% regroup %} tags, and I have the feeling that it is one of the reason why it is so slow. Moreover, it is very complex to maintain like this. Do you have any advise and design patterns to make it faster and more sustainable? I'm open to any suggestion, including moving the logic to models or creating template tags...
Here is a snippet of code, with only the structure, to give you a taste of what it looks like...
{% extends "base.tex" %}
{% block content %}
{% for courseoutline in courseoutline_list %}
...
{% regroup courseoutline.coursemembership_set_by_teaching_unit by teaching_unit as course_list %}
{% for course in course_list %}
{% regroup course.list|dictsort:"course.title" by course.title as course_sublist %}
{% for course in course_sublist %}
{% with course.list|semester:1 as cc %}
...
{% for c in cc %}
...
{% endfor %}
{% endwith %}
{% with course.list|semester:2 as cc %}
{% for c in cc %}
{% endfor %}
{% endwith %}
{% endfor %}
{% endfor %}
{% regroup courseoutline.coursemembership_set_by_semester by semester as semester_list %}
{% for semester in semester_list %}
{% regroup semester.list by teaching_unit as ue_list %}
{% for ue in ue_list %}
{% for course in ue.list|dictsort:"code" %}
{% endfor %}
{% endfor %}
{% endfor %}
{% endfor %}
{% endblock %}
Thanks!

Django regroup not working as expected

I have the following view in my django application
def ViewSale( request ):
salecur = Sale.objects.filter(user=2).order_by('sale_date')
return render_to_response('myapp/sale.html',{'salecur':salecur})
my template looks like this
{% regroup salecur by sale_date as sale_list %}
<ul>
{% for sale_date in sale_list %}
<li>{{ sale_date.grouper }}
<ul>
{% for sale in sale_list %}
<li>{{ sale.item }} - {{ sale.qty }} </li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
When i render the page i get the grouper sale_date.grouper printed, but {{ sale.item }} and {{ sale.qty }} in the inner loop shows nothing! Blank.
What am i missing?
Gath
{% regroup salecur by sale_date as sale_list %}
<ul>
{% for sale_date in sale_list %}
<li>{{ sale_date.grouper }}
<ul>
{% for sale in sale_date.list %}
<li>{{ sale.item }} - {{ sale.qty }} </li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
See the documentation on regroup:
{% regroup %} produces a list of group objects. Each group object has two attributes:
grouper -- the item that was grouped by (e.g., the string "Male" or "Female").
list -- a list of all items in this group (e.g., a list of all people with gender='Male').