1

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)
Reed Jones
  • 1,367
  • 15
  • 26

2 Answers2

0

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

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

Henrik Andersson
  • 45,354
  • 16
  • 98
  • 92
  • thanks, now the url seems to work, but the view function gives me an error 'matching query does not exist.' – Reed Jones Aug 12 '14 at 13:38
  • It's because you're returning `s` unmodified in your `strip` function. `replace()` doesn't modify the original string so you're trying to find a title that's still slugified. – Henrik Andersson Aug 12 '14 at 13:40
0

I think your regex is failing because '-' is not considered a match for \w. See here: https://docs.python.org/2/library/re.html

joel goldstick
  • 4,393
  • 6
  • 30
  • 46