4

I am getting 2 querysets from db:

all_locations = Locations.objects.all()[:5]
rating = Rating.objects.all()[:5]
return render_to_response('index.html',{'all':all_locations,'rating':rating},context_instance=RequestContext(request))

But I am stuck here, not knowing how to loop over these 2 querysets in one loop. this is being wrong:

{% if all and rating %}
  {% for every in all and rating  %}
         {{every.locationname}}, {{every.rating_score}}
  {% endfor %}
{% endif %}
3
  • Are they of equal length? Commented Mar 26, 2013 at 2:57
  • @JasonSperske, yeah, they are of equal length Commented Mar 26, 2013 at 2:58
  • btw: it is saying: Could not parse the remainder: '[all_locations,bewertung]' from '[all_locations,bewertung]' if i do for (a,b) in [all_locations,bewertung]. Commented Mar 26, 2013 at 3:22

3 Answers 3

5

You can try zip(all_locations, rating). It will produce a list of tuples. Then you can iterate over them in pairs. Here is an example: (demo)

all_locations = ['ca','ny','fl']
ratings = ['best','great','good']
for (l,r) in zip(all_locations,ratings): 
   print l+':'+r 

Outputs

ca:best
ny:great
fl:good
Sign up to request clarification or add additional context in comments.

3 Comments

like this? : for (a,b) in list
thanks, but i am getting error if i do: {% for (loc,bew) in zip(all_locations,bewertung) %}. ERROR: Could not parse the remainder: '(all_locations,bewertung)' from 'zip(all_locations,bewertung)'
According to the Django docs: docs.djangoproject.com/en/dev/topics/templates you can't invok zip from within the Django templating language, but you can zip in the views.py and expose the list as a variable to your template.
4

I have also come across this problem. Now I've fixed it. what I do is using

new=tuple(zip(queryset1,queryset2))  
return render(request, 'template.html', {"n": new}).

in view.py.

In template.html, I use three for sentences which are list below.

{% for i in n %}

{% for j in i|slice:"0:1" %}

......operate queryset1

{% endfor %}
{% for z in i|slice:"1:2" %}

.....operate queryset2


{% endfor %}

{% endfor %}

It seems this method will fulfill your needs.

1 Comment

Whats the process of doing if they are not equal in length??
0

this might work:

{% with rating|length as range %}
    {% for _ in range %}
        {{ rating[forloop.counter] }}
        {{ location[forloop.counter] }}
    {% endfor %}
{% endwith %}

i'm not sure if rating|length will to the job... you might need to add rating|length|times' withtimes` filter defined as:

@register.filter(name='times') 
def times(number):
    return range(number)

2 Comments

so i should put it between this? {% if all_locations and rating %} .. {% endif %}
You can't iterate over range neither can access a list item by index in that way. What you can do is create a templatetag that returns an element from the list by forloop counter.

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.