I have two url patterns in Django:
我在Django中有两个url模式:
urlpatterns += patterns('',
url(r'^(?P<song_name>.+)-(?P<dj_slug>.+)-(?P<song_id>.+)/$', songs.dj_song, name='dj_song'),
url(r'^(?P<song_name>.+)-(?P<artist_slug>.+)-(?P<song_id>.+)/$', songs.trending_song, name='trending_song'),
)
When I visit a URL of the first pattern, it opens it correctly. However if I try and visit a URL of the second pattern, it tries to access the first view again. The variables song_name
, dj_slug
, artist_slug
are strings and song_id
is an integer.
当我访问第一个模式的URL时,它会正确打开它。但是,如果我尝试访问第二个模式的URL,它会尝试再次访问第一个视图。变量song_name,dj_slug,artist_slugare strings和song_id是整数。
What should be the URL patterns for such a case with similar URL structure?
这种具有类似URL结构的案例的URL模式应该是什么?
1 个解决方案
#1
1
Both urls use the same regex. I removed the group names and get:
两个网址都使用相同的正则表达式。我删除了组名并得到:
url(r'^(.+)-(.+)-(.+)/$', songs.dj_song, name='dj_song'),
url(r'^(.+)-(.+)-(.+)/$', songs.trending_song, name='trending_song'),
Of course django uses the first match.
当然django使用第一场比赛。
You should use different urls for different views. For example add the prefix to the second url:
您应该为不同的视图使用不同的URL。例如,将前缀添加到第二个URL:
url(r'^trending/(?P<song_name>.+)-(?P<artist_slug>.+)-(?P<song_id>.+)/$',
songs.trending_song, name='trending_song'),
#1
1
Both urls use the same regex. I removed the group names and get:
两个网址都使用相同的正则表达式。我删除了组名并得到:
url(r'^(.+)-(.+)-(.+)/$', songs.dj_song, name='dj_song'),
url(r'^(.+)-(.+)-(.+)/$', songs.trending_song, name='trending_song'),
Of course django uses the first match.
当然django使用第一场比赛。
You should use different urls for different views. For example add the prefix to the second url:
您应该为不同的视图使用不同的URL。例如,将前缀添加到第二个URL:
url(r'^trending/(?P<song_name>.+)-(?P<artist_slug>.+)-(?P<song_id>.+)/$',
songs.trending_song, name='trending_song'),