Python Can';t访问子查询';Django模板中的属性。我可以打印每个项目';不过她叫什么名字
在此视图中,我将向其传递用户最喜爱的相册和歌曲:Python Can';t访问子查询';Django模板中的属性。我可以打印每个项目';不过她叫什么名字,python,django,Python,Django,在此视图中,我将向其传递用户最喜爱的相册和歌曲: @login_required def my_favorites(request): fav_albums = request.user.album_set.all() fav_songs = request.user.song_set.all() return render(request, "billyjoel/my_favorites.html", {"fav_albums": fav_albums, "fav_s
@login_required
def my_favorites(request):
fav_albums = request.user.album_set.all()
fav_songs = request.user.song_set.all()
return render(request, "billyjoel/my_favorites.html", {"fav_albums": fav_albums, "fav_songs" : fav_songs})
它基本上是我项目中的“我的帐户”页面
我会反复阅读每首歌(“fav_歌曲”),然后,对于每首歌,我会反复阅读该歌所在的所有专辑
{% if fav_songs.count > 0 %}
...
{% for song in fav_songs %} <!-- No colon after "fav_songs" -->
...
{% for albumsong in song.albums.all %}
<BR/><I>Number {{ albumsong.sequence_num }} on
{#<A HREF="{% url 'album_detail' albumsong.id %}">#}{{ albumsong.name }}</A></I>
{% endfor %}
my_favorites.html
{% extends "base.html" %}
{% load bj_filters %}
{% block title %}My Favorites{% endblock %}
{% block content %}
<H1>Billy Joel Album Browser: Favorite albums and songs for user {{ user. }} </H1>
<H1>Albums</H1>
{% if fav_albums.count > 0 %}
<TABLE ALIGN="center" WIDTH="100%" BORDER="1" CELLSPACING="0" CELLPADDING="4" BGCOLOR="#EEEEEE"><TR ALIGN="center" VALIGN="middle">
<TD><B><U>Title</U></B><BR><I><FONT SIZE="-1">(click a title to view its song list)</FONT></I></TD>
<TD><B><U>Released</U></B></TD>
<TD>Officiality</TD>
<TD>Concert</TD>
<TD>Wiki</TD>
<TD>Favorite?</TD>
{% for album in fav_albums %} <!-- No colon after "fav_albums" -->
</TR><TR>
<TD VALIGN="top">
{% if album.thumbnail %}
<img src="/static/{{ album.thumbnail }}" width="25"/>
{% else %}
<img src="/static/images/white_block.jpg" width="25"/>
{% endif %}
<a href="/albums/get/{{ album.id }}{{ url_params }}">{{ album.title }}</a>
{% if album.description %}
<BR/><FONT SIZE="-1"><I>{{ album.description|truncatewords:10 }}</I></FONT>
{% endif %}
<TD>{{ album.pub_date|date:"m/y" }}</TD>
<TD><IMG SRC="/static/images/{{ album.officiality|multival_to_str:"J,I,U->major,minor,unofficial,broken_image"}}.jpg" height="20"/></TD>
<TD>{{ album.is_concert|yesno:"Yes,No" }}</TD>
<TD><A HREF="{{ album.main_info_url }}">Wiki</A></TD>
<TD><A HREF="{% url 'toggle_album_like_go_to_favs' album.id %}"><I>unfavorite</I></A></TD>
{% endfor %}
</TR></TABLE>
{% else %}
<P><I>You have no favorite albums.</I></P>
{% endif %}
<H2>Songs</H2>
{% if fav_songs.count > 0 %}
<TABLE ALIGN="center" WIDTH="100%" BORDER="1" CELLSPACING="0" CELLPADDING="4" BGCOLOR="#EEEEEE"><TR ALIGN="center" VALIGN="middle"><TR>
<TD><B><U>Len</U></B></TD>
<TD><B><U>Name</U></B></TD>
<TD><B><U>Lyrics</U></B></TD>
</TR>
{% for song in fav_songs %} <!-- No colon after "fav_songs" -->
<TR>
<TD>{% if song.get_length_desc_from_seconds == "-1" %}
<I>n/a</I>
{% else %}
{{ song.get_length_desc_from_seconds }}
{% endif %}
</TD>
<TD>{{ song.name }}
{% if song.description %}
<BR/><FONT SIZE="-1"><I>{{ song.description|truncatewords:50 }}</I></FONT>
{% endif %}
{% for albumsong in song.albums.all %}
<BR/><I>Number {{ albumsong.sequence_num }} on {#<A HREF="{% url 'album_detail' albumsong.id %}">#}{{ albumsong.name }}</A></I>
{% endfor %}
</TD>
<TD>
{% if song.lyrics_url %}
<A HREF="{{ song.lyrics_url }}">Lyrics</A> (direct link)
{% else %}
<A HREF="https://www.google.com/webhp?q=#q=site:lyrics.wikia.com+billy+joel+{{ song.name|urlencode }}">Lyrics</A> (search)
{% endif %}
</TD>
</TR>
{% endfor %}
</TABLE>
{% else %}
<P><I>You have no favorite songs.</I></P>
{% endif %}
{% endblock %}
{%extends“base.html”%}
{load bj_过滤器%load}
{%block title%}我的收藏夹{%endblock%}
{%block content%}
比利·乔尔相册浏览器:用户{{user.}最喜爱的相册和歌曲
相册
{如果fav_albums.count>0%,则为%0}
标题
(单击标题可查看其歌曲列表)
释放
官方
音乐会
维基
最喜欢的
{fav_albums%}中的相册为%
{%if album.thumboil%}
{%else%}
{%endif%}
{%if album.description%}
{{album.description}truncatewords:10}
{%endif%}
{{album.pub_date}日期:“m/y”}
大调、小调、非正式、不完整的_image“}}.jpg“height=”20“/>
{{album.is|u concert}是否:“是,否”}
{%endfor%}
{%else%}
您没有最喜欢的专辑。
{%endif%}
歌曲
{如果fav_songs.count>0%,则为%0}
伦恩
名称
抒情诗
{fav_歌曲中歌曲的百分比%}
{%if song.get_length_desc_from_seconds==“-1”%}
不适用
{%else%}
{{song.get_length_desc_from_seconds}}
{%endif%}
{{song.name}
{%if song.description%}
{{song.description | truncatewords:50}
{%endif%}
{song.albums.all%中的albumsong的%
上的编号{{albumsong.sequence_num}}{#
{%endfor%}
{%if song.lymps_url%}
(直接链接)
{%else%}
(搜索)
{%endif%}
{%endfor%}
{%else%}
您没有最喜欢的歌曲。
{%endif%}
{%endblock%}
明白了。这个
{% for albumsong in song.albums.all %}
<BR/><I>#{{ albumsong.sequence_num }} on
<A HREF="{% url 'album_detail' albumsong.id %}">{{ albumsong.name }}</A></I>
{% endfor %}
{%用于song.albums.all%中的albumsong%
{{albumsong.sequence_num}on
{%endfor%}
需要换成这个
{% for albumsong in song.albumsong_set.all %}
<BR/><I>#{{ albumsong.sequence_num }} on
<A HREF="{% url 'album_detail' albumsong.album.id %}">{{ albumsong.album.title }}</A></I>
{% endfor %}
{%forAlbumSong中的albumsong.albumsong\u set.all%}
{{albumsong.sequence_num}on
{%endfor%}
我希望有诊断错误消息来帮助解决这类问题,而不仅仅是打印空字符串
{% for albumsong in song.albumsong_set.all %}
<BR/><I>#{{ albumsong.sequence_num }} on
<A HREF="{% url 'album_detail' albumsong.album.id %}">{{ albumsong.album.title }}</A></I>
{% endfor %}