【发布时间】:2010-04-17 23:02:45
【问题描述】:
我有一个网络应用程序,当点击相应的链接时,它会列出我所有的艺术家、专辑和歌曲。我广泛使用通用视图(object_list/detail)和命名的 url,但我遇到了麻烦。我有三个模板,它们几乎输出完全相同的 html,如下所示:
{% extends "base.html" %}
{% block content %}
<div id="content">
<ul id="starts-with">
{% for starts_with in starts_with_list %}
<li><a href="{% url song_list_x starts_with %}">{{ starts_with|upper }}</a></li>
{% endfor %}
</ul>
<ul>
{% for song in songs_list %}
<li>{{ song.title }}</li>
{% endfor %}
</ul>
</div>
{% endblock content %}
我的艺术家和专辑模板看起来几乎相同,我想将这三个模板合二为一。我的变量以song 开头的事实可以很容易地更改为默认的obj。这是我的<ul id="starts-with"> 命名网址,我不知道如何更正。显然,我希望它使用我的urls.py 中的命名网址链接到特定的专辑/艺术家/歌曲,但我不知道如何让它感知上下文。有什么建议吗?
urls.py:
urlpatterns = patterns('tlkmusic.apps.tlkmusic_base.views',
# (r'^$', index),
url(r'^artists/$', artist_list, name='artist_list'),
url(r'^artists/(?P<starts_with>\w)/$', artist_list, name='artist_list_x'),
url(r'^artist/(?P<artist_id>\d+)/$', artist_detail, name='artist_detail'),
url(r'^albums/$', album_list, name='album_list'),
url(r'^albums/(?P<starts_with>\w)/$', album_list, name='album_list_x'),
url(r'^album/(?P<album_id>\w)/$', album_detail, name='album_detail'),
url(r'^songs/$', song_list, name='song_list'),
url(r'^songs/(?P<starts_with>\w)/$', song_list, name='song_list_x'),
url(r'^song/(?P<song_id>\w)/$', song_detail, name='song_detail'),
)
【问题讨论】:
标签: python django django-templates