title: Django ManyToMany Template Questions
body: <p>Good Morning All,</p>
  
  <p>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.</p>
  
  <p>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?</p>
  
  <p>My Django Model: (most of the fields have been removed)</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>So, there is the basic models I'm using in my application called "recipes."<br>
  With that said, there are two questions I'm looking for answers on:</p>
  
  <ol>
  <li>How would I go about displaying the categories for a recipe on it's details page?</li>
  <li>How would I go about displaying the image for the recipe on it's details page?</li>
  </ol>
  
  <p>If I go into the Python shell, and input the following, I do get a result:</p>
  
  <pre><code>&gt;&gt;&gt; photos = Photo.objects.filter(recipe=1)
  &gt;&gt;&gt; photos
  [&lt;Photo: Awesome Pasta&gt;]
  &gt;&gt;&gt; for photo in photos:
  ...     print "Photo: %s" % photo.logo
  ... 
  Photo: images/recipes/2550298482_46729d51af__.jpg
  </code></pre>
  
  <p>But when I try something like the following in my template, I get an error saying "Invalid block tag: 'photo.image'."</p>
  
  <pre><code>{% for photo in photos %}
  {% photo.image %}
  {% endfor %}
  </code></pre>
  
  <p>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?</p>
  
  <p>Details Page View.py snippet:</p>
  
  <pre><code>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})
  </code></pre>
  
  <p>Thanks in advance for the help and understanding for what is probably a very simple question to all of you!</p>
  
  <p><strong>UPDATE:  When removing the additional fields in the models, I forgot the categories field for the Recipes model.</strong>  </p>