Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
900 views
in Technique[技术] by (71.8m points)

regex - Regular expression in URL for Django slug

I have 2 URL's with a slug field in the URL.

url(r'^genres/(?P<slug>.+)/$', views.genre_view, name='genre_view'),
url(r'^genres/(?P<slug>.+)/monthly/$', views.genre_month, name='genre_month'),

The first one opens fine but the second one gives a DoesNotExist error saying Genres matching query does not exist.

Here is how I'm accessing the 2nd URL in my HTML

<li><a href="{% url 'genre_month' slug=genre.slug %}">Monthly Top Songs</a></li>

I tried to print the slug in the view. It is passed as genre_name/monthly instead instead of genre_name.

I think the problem is with the regex in the URLs. Any idea what's wrong here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Django always uses the first pattern that matches. For urls similar to genres/genre_name/monthly your first pattern matches, so the second one is never used. The truth is the regex is not specific enough, allowing all characters - which doesn't seem to make sense.

You could reverse the order of those patterns, but what you should do is to make them more specific (compare: urls.py example in generic class-based views docs):

url(r'^genres/(?P<slug>[-w]+)/$', views.genre_view, name='genre_view'),
url(r'^genres/(?P<slug>[-w]+)/monthly/$', views.genre_month, name='genre_month'),

Edit 2020:

Those days (since Django 2.0), you can (and should) use path instead of url. It provides built-in path converters, including slug:

path('genres/<slug:slug>/', views.genre_view, name='genre_view'),
path('genres/<slug:slug>/monthly/', views.genre_month, name='genre_month'),

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...