0

I have two url patterns in Django:

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_slugare strings and song_id is an integer.

What should be the URL patterns for such a case with similar URL structure?

1
  • sample urls you tried to access your views with would be useful - maybe then you would realize what was wrong in the first place :) Commented Mar 10, 2015 at 15:18

1 Answer 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.

You should use different urls for different views. For example add the prefix to the second url:

url(r'^trending/(?P<song_name>.+)-(?P<artist_slug>.+)-(?P<song_id>.+)/$',
                                  songs.trending_song, name='trending_song'),
Sign up to request clarification or add additional context in comments.

2 Comments

So it isn't possible to use the same URL pattern for 2 different views?
Yes, it is not possible. Yesterday-Beatles-123 - is this the DJ Song of Trending Song? :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.