Put items on top in django template for loop - django

The model has a field named "is_highlighted", i want to put all items on top if is_highlighted == True when iterate thru the object list.

You can give 2 for loops to do this. In first for loop check the condition true.
{% for x in list %}
{% if x.is_highlighted == True %}
....
..
{% endif %}
{% endfor %}
display others in second for loop
{% for x in list %}
{% if x.is_highlighted != True %}
...
...
You can also pass 2 query sets through context. I don't know if it's best or any other way. simply this will work.

Related

Detect if a forloop has zero loops

I'm trying to handle a case where a forloop with a where clause results in zero loops.
I've tried using set and map in various ways unsuccessfully, possibly one of those is the solution but I just couldn't get it right.
{% for variable in type.allVariables where variable.type.implements["SomeProtocol"]["name"] == "SomeProtocol" %}
// Add code for each variable
{% endfor %}
// Add backup code if forloop didn't do a single loop
Found the answer in the stencil documentation of all places - who knew!
The for tag can take an optional {% empty %} block that will be displayed if the given list is empty or could not be found.
{% for user in users %}
<li>{{ user }}</li>
{% empty %}
<li>There are no users.</li>
{% endfor %}

empty tag {% empty %} in django template doesn't work when "if" condition is nested inside for loop

<ul>
{% for entry in entries %}
{% if title in entry %}
<li>{{ entry }}</li>
{% endif %}
{% empty %}
<li>Sorry! No result matches your search.</li>
{% endfor %}
</ul>
This is my html template.
Want to create a list where the list items have to meet certain condition, so I nested if condition inside for loop. The list works, but empty tag doesn't.
When the list is empty, it doesn't show anything.
How can I make it work?
You seem to have misunderstood the {% empty %} tag. It only works if entries is empty or not found. It doesn't matter if any HTML is generated inside the for loop, so it is independent of your if clause.
What you can do is to filter entries in your view code to only return entries with title in them.

Django template iteration / loop count based on condition

I'm trying to create an iteration like the a $i==0; $i++; from PHP in Django, based on a condition.
{% for item in event.products %}
{% if item.category = "Treat" %}
Now - I want to be able to tell how many times has this condition been met (category = treat) and how to stop the for loop after 2 items that match that loop.
Thanks!
django template system does not permit breaks in for loops nor setting counters, even if elsewhere it is shown how to overcome this limitation in some cases or how to create new template tags that could help you, maybe you can calculate beforehand your requirements and prepare the list to be printed by slicing it in your view.
I agree with #DRC that this business logic is best done in your view code and not in the template.
If you still need a template solution:
{% regroup event.products by item.category as grouped_products %}
{% for group in grouped_products %}
{% if group.grouper == "Treat" %}
{% for item in group.list|slice:":2" %}
{{ item.imageURL }}
{% endfor %}
{% endif %}
{% endfor %}
Documentation for slice and regroup.

Check if object is in manytomany list in template

How can I check if a certain object/id is in a list?
I want something to be displayed if the ID of the connected object is not "6".
Tried with something like this:
{% if user.benefits.all != "6" %}
You do not have a benefit with ID 6.
{% endif %}
It is better to not put much logic into templates. View (or model) - is a better place for that.
For example in view you can check, that user.benefits has element with id=6 by this code:
has_benefit = user.benefits.filter(id=6).count() > 0
context['has_benefit'] = has_benefit
Now in template just use this new context variable:
{% if not has_benefit %}
You do not have a benefit with ID 6.
{% endif %}
UPDATED:
If you still want to do it in template, it is better to create a custom template filter:
from django import template
register = template.Library()
#register.filter(name='has_benefit')
def has_benefit(user, benefit_id):
b_id = int(benefit_id)
return user.benefits.filter(id=b_id).count() > 0
Now in template load your templatetags module using {% load module_name %} and use:
{% if not user|has_benefit:"6" %}
You do not have a benefit with ID 6.
{% endif %}
{% for benefit in user.benefits.all %}
{% if benefit.id != 6 %}
You do not have a benefit with id 6
{% endif %}
{% endfor %}
But this will loop through all the benefits and print it every time the condition passes.
So, you should write a template tag which returns you a list of ids for all benefits for a particular user and once you have that list you can do:
{% if 6 not in list_of_benefit_ids %}
You do not have a benefit with id 6
{% endif %}

How could I access the data of this dict from a django template?

I have a dict with entries such as this:
d[(1,2,3)] = True
d[(4,5,6)] = False
How could I access them from the django template?
Indexing the dictionary by a tuple in the template is not possible, but for your particular case, you can do this:
{% for key,value in dictionary.items %}
{% if key.0 == 1 and key.1 == 2 and key.2 == 3 %}
{{ value }}
{% endif %}
{% endfor %}
In the end, I wrote a custom tag, because it feels too weird, and it's probably much slower to do the comparison in each iteration.