Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6074421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:22:48+00:00 2026-05-23T10:22:48+00:00

I need to iterate through a tuple in the template, but from the code

  • 0

I need to iterate through a tuple in the template, but from the code i’ve built i am getting a tuple of an object (album) and a queryset (photo). The problem is how do i iterate over them now in the template?

models:

class Album(models.Model):
    title = models.CharField('כותרת', max_length=100, db_index=True)
    created = models.DateTimeField('תאריך פרסום', auto_now_add=True)
    creator = models.ForeignKey(User, related_name='galleries_creator', verbose_name='נכתב ע"י')

class Photo(models.Model):
    title = models.CharField('כותרת', max_length=100)
    album = models.ForeignKey(Album, verbose_name='שייך לאלבום')
    photo = models.ImageField('תמונה', blank=True, upload_to=get_image_path)
    photo_mid = models.ImageField('תמונה בינונית', blank=True, upload_to='images/galleries/mid/', editable=False)
    photo_thumb = models.ImageField('תמונה קטנה', blank=True, upload_to='images/galleries/thumbs/', editable=False)
    created = models.DateTimeField('תאריך פרסום', auto_now_add=True)
    is_landscape = models.NullBooleanField(blank=True, verbose_name='האם תמונת לנדסקייפ', editable=False)

View:

def index(request):
    albums = Album.objects.all().order_by('?')[:10]
    album_list = []
    for album in albums:
        album_list.append((album, Photo.objects.filter(album=album).order_by('?')[:1]))
    return render_to_response('galleries/index.html',{'albums':album_list},  context_instance=RequestContext(request))

template:

{% for album, photo in albums %}
    <div class="polaroid" id="picture_{{ forloop.counter }}">
        <img src="{{ MEDIA_URL }}{{ photo.photo }}" alt="Picture #{{ forloop.counter }}" />
        <p class="caption"><p class="title">אלבום {{ forloop.counter }}</p><p>{{ album.creator.first_name }} {{ album.creator.last_name }}</p>
    </div>
{% endfor %}

As i see it, i can’t do photo.photo because photo is a list, not an object and i really don’t want to do a for loop for every photo in the tuple (there is only one photo, so this is just a waste).

assert False album_list returns:

[(<Album: אלבום שני - ממשק מנהל>, [<Photo: אלבום 2 תמונה 2>]), (<Album: אלבום ראשון - ממשק מנהל>, [<Photo: אלבום 1 תמונה 7>]), (<Album: אלבום  נתן>, [<Photo: נסיון 4>])]

What can i do in my view or template to solve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:22:48+00:00Added an answer on May 23, 2026 at 10:22 am

    If there’s only one photo per album you are after, then why slice the queryset? Just get the first one using index:

    album_list.append((album, Photo.objects.filter(album=album).order_by('?')[0]))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to iterate through a bunch of dynamically generated fields, but this doesn't
I need to iterate through the items (strings) in a CComboBox to check which
I need to iterate through the fields on a table and do something if
I've been working on a project where I need to iterate through a collection
I need to create feature which will iterate through all subsites of site collection
I need to iterate through SortedMap's entry set (which is a SortedSet) backwards. The
I need to iterate through a LinkedList<T> (in .NET 2.0) and remove all the
I need to iterate through a DataTable . I have a column there named
I have a Dictionary whose elements I need to iterate through and make changes.
I need to iterate through a dynamic array. The array will look something like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.