0

I want to get the list of songs under the list of artists. My artist class just contains the artists first and last name. My song class contains a foreign key of an artist along with the song title. I am able to list the artists but when I try to list the songs of the artist I get and error in my {% endif %} that ends my if statement {% if song %}.

{% extends "base.html" %}
    {% block heading %}Music Catalog{% endblock %}
    {% block content %}
        {% if user.username %}
            <p>Welcome {{ user.username }}!</p>
            {% if artist %}
                <u1>
                    {% for singer in artist %}
                        <li>{{ singer.firstname }} {{ singer.lastname }}</li>
                        {% if song %}
                            <u1>
                                {% for songs in song %}
                                    {% if (songs.artist.firstname == singer.firstname
                                     and songs.artist.lastname == singer.lastname) %}
                                        <li>{{ songs.title }}</li>
                                    {% endif %}
                                {% endfor %}
                            </u1>
                        {% endif %}
                    {% endfor %}
                </u1>
            {% else %}
                <p>No artists were found in the music catalog.</p>
            {% endif %}
        {% else %}
            <p>You need to <a href="/login/">login</a> to see your music catalog.</p>
        {% endif %}
    {% endblock %}

    enter code here
3
  • 1
    Do yo urealize that your <ul> tags are actually typed as <u1> (note the number 1 not l)? Commented Dec 6, 2012 at 1:03
  • oh it should be 'ul'? haha I never realized that, I guess I just that l was 1 makes sense though since ul means unordered list. Commented Dec 6, 2012 at 1:06
  • Are you passing song from the view code? If yes, then you are going to display the same songs for all the Artist! Commented Dec 6, 2012 at 1:06

4 Answers 4

1

I don't think so, in the template language if statement you can use round bracket it won't parse. Try by removing it...

{% if songs.artist.firstname == singer.firstname and songs.artist.lastname==singer.lastname%}
Sign up to request clarification or add additional context in comments.

Comments

0

It seems that your view should be handling more of this logic. Like Raunak Agarwal mentioned if you are passing your song or songs in to the template then each one is going to be the same.

It's very strange as well to be doing a

{% for songs in song %}

That just doesn't read right.

I would visit the view a little closer. I had written some more below. After looking over your code though taking a look at the view as well as the model would shed some light on things and allow for a much better help/answer to be given.

Comments

0

As you said My song class contains a foreign key of an artist along with the song title. - why don't you just use the regroup feature?

{% regroup song by artist as artist_list %}

<ul>
{% for artist in artist_list %}
    <li>{{ artist.grouper }}
    <ul>
        {% for songs in artist.list %}
          <li>{{ songs.title }}</li>
        {% endfor %}
    </ul>
    </li>
{% endfor %}
</ul>

Comments

0

Yes, your if song line is incorrect. It's quite clear from the template that you don't even have a song attribute at that point. Where is it supposed to be coming from? Presumably it's a related set on singer, but you haven't said so in the template.

You probably want something like this:

{% for singer in artist %}
    <li>{{ singer.firstname }} {{ singer.lastname }}
    {% with songs as singer.song_set.all %}
        {% if songs %}
            <ul>
                {% for song in songs %}
                    <li>{{ song.title }}</li>
                {% endfor %}
            </uL>
        {% endif %}
    {% endwith %}
    </li>
{% endfor %}

I've also removed that comparison of artist firstname and lastname, that didn't seem to make sense: you're already iterating through the set of songs from that artist, so no comparison is needed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.