Search code examples
regexdjangodjango-urlsurlconf

(django URLconf) page not found error only for urls with '-' character


I have a weird problem can't figure out whats wrong, I get 404 errors for any url that contains a '-' character...

my urls.py in the project root works fine

url(r'^serialy/', include('movieSite.series.urls')),

next, is

urlpatterns = patterns('',

              url(r'^$', views.serialy_index, name='serialy_index'), #this works 

              (r'^(?P<serial_title>[\w]+)/$', serial),

              )

The second one, using serial_title, works only if the series' title is something like, 'Dexter,' or 'Continuum.' But other series have titles like 'Family guy,' so when I create the url I use a function that changes it to 'Family-guy,' but for some reason it won't work for those titles with the '-' characters. I always get a 404 error like this

Using the URLconf defined in movieSite.urls, Django tried these URL patterns, in this order:

^serialy/ ^$ [name='serialy_index']
^serialy/ ^(?P<serial_title>[\w]+)/$

^static\/(?P<path>.*)$
The current URL, serialy/Whats-with-Andy/, didn't match any of these.

so here the url serialy/whats-with-andy/ doesn't match, but if I visit serialy/continuum it works fine?? anyone have any ideas as to what could cause this? Oh and this is what the view looks like

def strip(s):
    s.replace('-',' ')
    return s
def serial(request, serial_title=None) :
    s_title = strip(serial_title) 
    obj = Show.objects.get(title__icontains=s_title)
    #episodes = obj.episodes
    des = obj.description 
    img = obj.image 
    title = obj.title 

    t = get_template('serial.html')
    html = t.render(Context({
                             'the_title':title,'the_image':img,'the_description':des
                             })) 
    return HttpResponse(html)

Solution

  • The regex [\w]+ only matches words and not special chars like -.

    If you change it to [-\w]+ it'll match "slug" urls.