accessing foreignKey inside the template

django, django-models, django-templates

Solution

Try changing your template code to:

{% for song in song_list %}
  {% for artist in song.artists.all %}
    {{ artist.name }} - {{ song.title }} 
  {% endfor %}
{% endfor %}  

Problem

well it's quiet simple. 2 models with ManyToMany relation: ``` class Artist(models.Model): name = models.CharField(max_length=100, unique=True) slug = models.SlugField(max_length=100, unique=True, help_text='Uniq value for artist page URL, created from name') birth_name = models.CharField(max_length=100, blank=True) class Song(models.Model): title = models.CharField(max_length=255) slug = models.SlugField(max_length=255, unique=True, help_text='Unique value for product page URL, create from name.') youtube_link = models.URLField(blank=False) artists = models.ManyToManyField(Artist) ``` my view suppose to display latest 5 songs: ``` def songs(request, template_name="artists/songs.html"): song_list = Song.objects.all().order_by('-created_at')[:5] return render_to_response(template_name, locals(), context_instance=RequestContext(request)) ``` and the problem is in the template... i want to display the artist name but just dont realy know how to do it, i tried: ``` {% for song in song_list %} {{ artists__name }} - {{ song.title }} {% endfor %} ``` would appreciate any help !

Original source