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

  • SEARCH
  • Home
  • 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 4619012
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:21:59+00:00 2026-05-22T02:21:59+00:00

I have set up a few models as follows: Page Gallery Image Gallery and

  • 0

I have set up a few models as follows:

Page

Gallery

Image

Gallery and Image share a many to many relationship.

Page can have 1 or 0 galleries.

I have created a gallery with a few images and attached it to a page object.

However, I’m having trouble displaying it in a template.

Can anyone help?

My page view looks like this:

def detail(request, page_id):
p = get_object_or_404(Page, pk=page_id)
return render_to_response('page/detail.html', {'page': p},
                      context_instance=RequestContext(request))

And the template:

{% block images %}
{% if page.gallery %}
{% for image in page.gallery.image_set.all %}
<a rel="gallery" href="{{ STATIC_URL }}{{ image.image }}"></a>
{% endfor %}
{% endif %}
{% endblock %}`

If I try to output some text within the IF block it comes out OK but not within the loop.

Any advice on getting it to work / generally tidying this up would be greatly appreciated.

Thanks.

  • 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-22T02:22:00+00:00Added an answer on May 22, 2026 at 2:22 am

    If the Gallery-Image relationship is many to many I’m guessing you have an m2m field on your Gallery model, where the m2m manager is the field itself. The manager can be queried just like any other QuerySet: model_instance.mym2mfield.filter().

    If your ManyToManyField is called images on the Gallery model, your template would look like this:

    {% for image in page.gallery.images.all %}
        <a rel="gallery" href="{{ STATIC_URL }}{{ image.image }}"></a>
    {% endfor %}
    

    If your ManyToManyField is on your Image model, the default name would be as you describe, unless you specified a related_name in which case you’d use that name.

    {% for image in page.gallery.image_set.all %}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have set up a few models as follows: Product Category Product and Category
I have set up a few models in a simple django project. When I
I have a page that takes a few minutes to run. When I set
I have a few models set up as Company and Employee. A company has
I have to set a few projects in our VS 2005 solution to build
I am using symfony 1.0 and have set routing for few URLs as below.
I have a set of checkboxes and have disabled few which I want to
I have a tableView with a few sections and I have it set for
In a QGraphicsScene , I have a background set with a few QGraphicsItem s
I have a few mappings in vim for moving between splits set in my

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.