Algorithmatic
Algorithmatic

Reputation: 1892

why is this django regex wont work?

I have this regex in my urls.py for my blog app and I'd like to know why is it not working.

url(r'^/tag/(?P<tag_text>\w+)/$', views.tag, name='tag'),

and I have defined this in the blog's views.py

def tag(request,tag_text):

and this in the application's urls.py

url(r'^blog/', include('blog.urls')),

I have tried localhost/blog/tag/sport but I still get: The current URL, blog/tag/sport, didn't match any of these. Am I doing something wrong?

Upvotes: 1

Views: 88

Answers (1)

asermax
asermax

Reputation: 3123

Your pattern is trying to match an extra /, since your include url requires a trailing slash, and your tag url is trying to match a leading slash.

You should remove either one to make it work:

# tag url in blog/urls.py
url(r'^tag/(?P<tag_text>\w+)/$', views.tag, name='tag'),

# include in project/urls.py
url(r'^blog/', include('blog.urls')),

Upvotes: 2

Related Questions