【发布时间】:2013-02-02 23:58:03
【问题描述】:
我的 Django 应用程序的 urls.py 文件的结构存在一些问题。
我的项目是一个基本的音乐播放器应用。您首先单击 Music 的链接,然后单击(例如)Artists,然后选择一个艺术家,例如 Weezer。然后,该应用会显示该艺术家的专辑和歌曲列表,由 views.artist_name 函数在 artist_name.html 模板上呈现。
到目前为止,在应用程序的导航中,URL 看起来像 http://localhost/music/artists/Weezer/。
我的问题在于下一个 URL 的编码。如果我选择 Weezer 的专辑 The Blue Album,我会返回 URL:http://localhost/music/artists/Weezer/The%20Blue%20Album。 p>
这应该使用 views.artist_album_title 函数呈现一个名为 artist_album_title.html 的模板。相反,它使用与当前页面相同的 views.artist_name 函数呈现新页面。
似乎正在发生的是 ...Weezer/The%20Blue%20Album/ 的正则表达式模式与我的 urls.py 文件中的相关 URL 模式不匹配。作为 Django 新手(并且对正则表达式的经验很少),我很难确定我的 urls.py 文件对于这种应用程序应该是什么样子。
下面是我目前的 urls.py 文件。欢迎对我的问题提供任何帮助。谢谢大家。
from django.conf.urls import patterns, include
from music.views import home, music, artists, artist_name, artist_album_title
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
(r'^$', home),
(r'^music/$', music),
(r'^music/artists/$', artists),
(r'^music/artists/([\w\W]+)/$', artist_name),
(r'^music/artists/([\w\W]+)/([\w\W]+)/$', artist_album_title),
)
来自views.py 的artist_album_title 函数
def artist_album_title(request, album_title):
album_tracks = Track.objects.filter(album__title=album_title)
return render_to_response('artist_album_title.html', locals(), context_instance=RequestContext(request))
【问题讨论】:
-
你需要SlugField。
标签: django django-urls