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 311955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:58:19+00:00 2026-05-12T07:58:19+00:00

Good Morning All, I’ve been a PHP programmer for quite some time, but I’ve

  • 0

Good Morning All,

I’ve been a PHP programmer for quite some time, but I’ve felt the need to move more towards the Python direction and what’s better than playing around with Django.

While in the process, I’m come to a stopping point where I know there is an easy solution, but I’m just missing it – How do I display manytomany relationships in a Django Template?

My Django Model: (most of the fields have been removed)

class Category(models.Model):
    name = models.CharField(max_length=125)
    slug = models.SlugField()   
categories = models.ManyToManyField(Category, blank=True, null=True)

class Recipe(models.Model):
    title = models.CharField('Title', max_length=250)
    slug = models.SlugField()

class Photo(models.Model):
    recipe = models.ForeignKey(Recipe)
    image = models.ImageField(upload_to="images/recipes", blank=True)

So, there is the basic models I’m using in my application called “recipes.”
With that said, there are two questions I’m looking for answers on:

  1. How would I go about displaying the categories for a recipe on it’s details page?
  2. How would I go about displaying the image for the recipe on it’s details page?

If I go into the Python shell, and input the following, I do get a result:

>>> photos = Photo.objects.filter(recipe=1)
>>> photos
[<Photo: Awesome Pasta>]
>>> for photo in photos:
...     print "Photo: %s" % photo.logo
... 
Photo: images/recipes/2550298482_46729d51af__.jpg

But when I try something like the following in my template, I get an error saying “Invalid block tag: ‘photo.image’.”

{% for photo in photos %}
{% photo.image %}
{% endfor %}

Although, even if that did work, the ID is still hard coded into the view, how would you go about having this dynamic for each recipe?

Details Page View.py snippet:

def details(request, slug='0'):
    p = get_object_or_404(Recipe, slug=slug)
    photos = Photo.objects.filter(recipe=1)
    return render_to_response('recipes/recipes_detail.html', {'p': p, 'photos': photos})

Thanks in advance for the help and understanding for what is probably a very simple question to all of you!

UPDATE: When removing the additional fields in the models, I forgot the categories field for the Recipes model.

  • 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-12T07:58:20+00:00Added an answer on May 12, 2026 at 7:58 am

    From what I can see, I think you’ve got a small syntax error:

    {% photo.image %}
    

    should instead be:

    {{ photo.image }}
    

    The {% %} notation is used for django template tags. Variables, on the other hand, are expressed with the {{ }} notation.

    To make it dynamic, you can take advantage of the fact that your Photo model has a foreign key to Recipe. This means that there will be a reverse relation from the Recipe instance you’ve loaded using the slug back to the set of photos:

    def details(request, slug='0'):
        p = get_object_or_404(Recipe, slug=slug)
        photos = p.photo_set.all()
    

    Hopefully that will work for you. Glad to see you’re enjoying working with Django!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.