1

is there a way to break out of this for loop from inside the if statement. Currently our database is incorrectly storing multiple primary phones and I would like to break out of the for loop after the first primary phone is found. Thank you in advance for any help.

{% for phone in user_phones %}
    {% if phone.primary %}
        <div>{% if phone.type %}{{ phone.type|title }}: {% endif %}<span itemprop="telephone">{{ phone.phone_format }}</span></div>
    {% endif %}
{% endfor %}

Updated:

Or just fail the if condition by creating a variable within the if true branch

3
  • possible duplicate of How to break "for loop" in Django template Commented Jun 2, 2015 at 19:19
  • This sounds more like business logic than a ui/template problem. I recommend preparing a specific context variable in your view. Commented Jun 2, 2015 at 19:26
  • 1
    @Wtower that question is not the same Commented Jun 2, 2015 at 19:29

2 Answers 2

2

If you have to stay within the template layer you could use regroup.

{% regroup user_phones|dictsort:"primary" by primary as phones_list %}

{% for phone in phones_list %}
    {% if phone.grouper %}
    {{ phone.list.0.type }}
    {% endif %}
{% endfor %}

What it does

regroup together with the dictsort filter (which also works on querysets) groups the instances in user_phones by the value of primary.

regroup will add an attribute named grouper, which when grouping by a bool (the value of primary) will either be True or False.

for then iterates over the variable phones_list, which is provided by regroup. Since we have sorted the results by primary, {% if phone.grouper %} will tell us when we hit the group of items with primary == True.

regroup packs the items that belong to a group into the attribute list. So the first item can be accessed with phone.list.0.type, phone.list.0.phone_format, etc.

Note:
if you need to access foo.list.0 many times it can be assigned to a variable (using with):

{% regroup user_phones|dictsort:"primary" by primary as phones_list %}

{% for items in phones_list %}
    {% if items.grouper %}
    {% with items.list.0 as phone %}
    <div>{% if phone.type %}{{ phone.type|title }}: {% endif %}<span itemprop="telephone">{{ phone.phone_format }}</span></div>
    {% endwith %}
    {% endif %}
{% endfor %}
Sign up to request clarification or add additional context in comments.

Comments

0

There is no break in Django templates. You may handle it in your view by storing the primary phone that you are looking for to a variable and then calling it in your template.

5 Comments

So there's no way to do it from inside the if statement?
@Dusty: No, break is not possible in a Django template for loop. You'll have to process the data before passing it to the template.
@Dusty yes, there is no way. You should handle it in views.py.
Can you define variables in for loops in Django?
You cannot. You should do it in py file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.