Django:如何编写if语句

Django:如何编写if语句,django,django-templates,Django,Django Templates,我想得到艺术家名单下的歌曲列表。 我的艺术家类只包含艺术家的名字和姓氏。 My song类包含艺术家的外键和歌曲标题。 我可以列出艺术家,但当我尝试列出我得到的艺术家的歌曲时,我的{%endif%}中出现错误,这结束了我的if语句{%if song%} {% extends "base.html" %} {% block heading %}Music Catalog{% endblock %} {% block content %} {% if user.use

我想得到艺术家名单下的歌曲列表。 我的艺术家类只包含艺术家的名字和姓氏。 My song类包含艺术家的外键和歌曲标题。 我可以列出艺术家,但当我尝试列出我得到的艺术家的歌曲时,我的{%endif%}中出现错误,这结束了我的if语句{%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
{%extends“base.html”%}
{%block heading%}音乐目录{%endblock%}
{%block content%}
{%if user.username%}
欢迎{{user.username}}

{%if-artist%} {艺术家中歌手的百分比%}
  • {{singer.firstname}{{singer.lastname}}
  • {%if歌曲%} {对于歌曲%%中的歌曲,} {%if(songs.artist.firstname==singer.firstname) 和songs.artist.lastname==singer.lastname)%}
  • {{songs.title}
  • {%endif%} {%endfor%} {%endif%} {%endfor%} {%else%} 在音乐目录中找不到艺术家

    {%endif%} {%else%} 你需要看看你的音乐目录

    {%endif%} {%endblock%} 在这里输入代码
    我不这么认为,在模板语言if语句中,您可以使用圆括号,但它不会解析。尝试删除它

    {% if songs.artist.firstname == singer.firstname and songs.artist.lastname==singer.lastname%}
    

    看来你的观点应该更多地处理这种逻辑。就像Raunak Agarwal提到的,如果你将你的一首或多首歌曲传递到模板中,那么每首歌都是一样的

    这是非常奇怪的,以及正在做一个

    {% for songs in song %}
    
    这读起来不对

    我会更近一点看风景。我在下面又写了一些。查看代码后,尽管查看视图和模型会让您了解一些事情,并提供更好的帮助/答案。

    正如您所说,我的歌曲类包含一个艺术家的外键以及歌曲标题。-你为什么不直接使用这个功能呢

    {%将歌曲按艺术家重新组合为艺术家\u列表%}
    
      {艺术家列表中艺术家的百分比%}
    • {{artist.gropper}}
        {artist.list%中歌曲的%s}
      • {{songs.title}
      • {%endfor%}
    • {%endfor%}

    是,如果歌曲行不正确,您的
    。从模板中可以很清楚地看出,此时您甚至没有
    歌曲
    属性。它应该从哪里来?想必这是singer上的一个相关设置,但您在模板中没有这样说过

    你可能想要这样的东西:

    {% 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 %}
    
    {%为艺人中的歌手%}
    
  • {{singer.firstname}{{singer.lastname}} {%歌曲为singer.song_set.all%} {%if歌曲%}
      {%用于歌曲中的歌曲%}
    • {{song.title}
    • {%endfor%}
    {%endif%} {%endwith%}
  • {%endfor%}

    我还删除了艺术家名字和姓氏的比较,这似乎没有什么意义:你已经在重复该艺术家的歌曲集,因此不需要进行比较。

    你是否意识到你的
    标签实际上是键入的
    (注意数字1不是l)?哦,它应该是'ul'?哈哈,我从来没有意识到,我想我只是觉得我1岁是有道理的,因为ul的意思是无序列表。你是从视图代码传递歌曲吗?如果是,那么您将为所有艺术家显示相同的歌曲!
    {% 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 %}