Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django Basics Test Time Django TDD

Gilang Ilhami
Gilang Ilhami
12,045 Points

I need a more detail explanation about this

I was pretty much struggling with the Django TDD challenge for the last couple of days. I've been asking questions in the Community, but the lack of responses wasn't helping at all. Yes, after i went through a lot of trials and errors, i was able to find an answer and have a better understanding of both Django and Python. In the end, i passed the challenge.

But i was pretty much unsatisfied because there is one part of the challenge that i couldn't understand, even though i'v already found the solution.

{% extends 'base.html' %}

{% block title %}{{ performer }}{% endblock %}

{% block content %}
<h2>{{ performer }}</h2>

  {% for song in performer.song_set.all %}
      {% if song.performer.pk == performer.pk %}
        {{ song }}
      {% endif%}
  {% endfor %}
{% endblock %}

I don't really understand the function of the "for loop" and why it is being used that way. Can somebody please explain to me more detailed about this? or if there this any documentation that i can read about this?

Thank you very much :)

2 Answers

performer.song_set.all is a list of songs that the for loop is iterating through. even if there is only one song in the list, it'll still iterate that one song. The thing that would confuse me is the comparison of the songs performer to the performer (the if statement) when the song is from the performer's song_set. I would have to see what this bit of code is actually referring to to be able to answer that.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

I'm pretty sure that {% if %} doesn't need to be there. It shouldn't be doing anything.

Gilang Ilhami
Gilang Ilhami
12,045 Points

Hey Brandon, hey kenneth, thank you very much for the responses. It was pretty confusing at first, but after watching the "Customizing Django Templates" course, i got a better understanding of it.