Reputation: 915
I learn Django ORM. How can I get Photo in my template?
def index(request):
animal = Animal.objects.all()
return render_to_response('animal.html',
{'animal':animal,},
context_instance=RequestContext(request))
template:
{{ animal.name }}
{{ animal.photo.all }}
but this not working.
models:
class Animal(models.Model):
name = models.CharField(max_length=255)
class Photo(models.Model):
photo = models.ImageField(upload_to='media/images')
animal = models.ForeignKey(Animal)
Upvotes: 1
Views: 755
Reputation: 174624
You need to read the documentation on following relationships to see how do you access related items.
In your case, each animal
has a photo_set
which is a way to get a list of all the photo objects belonging to that animal.
In your template, you would do:
{{ animal.name }}
{% for picture in animal.photo_set.all %}
<img src="{{ picture.photo.url }}" />
{% endfor %}
Upvotes: 4