views:

883

answers:

1

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.

+3  A: 

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!

Jarret Hardie
Thanks so much for all of the help, it's much appreciated and my page now has images on it!
Oubipaws