Monday, 15 August 2011

Django Foreign key. Access in template -



Django Foreign key. Access in template -

i larn django orm. how can photo in template?

def index(request): animal = animal.objects.all() homecoming render_to_response('animal.html', {'animal':animal,}, context_instance=requestcontext(request))

template:

{{ animal.name }} {{ animal.photo.all }}

but 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)

you need read documentation on following relationships see how access related items.

in case, each animal has photo_set way list of photo objects belonging animal.

in template, do:

{{ animal.name }} {% image in animal.photo_set.all %} <img src="{{ picture.photo.url }}" /> {% endfor %}

django

No comments:

Post a Comment