1

我有一个奇怪的问题,无法弄清楚出了什么问题,对于任何包含“-”字符的 url,我都会收到 404 错误...

我在项目根目录中的 urls.py 工作正常

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

接下来是

urlpatterns = patterns('',

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

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

              )

第二个使用serial_title,仅在系列标题类似于“ Dexter ”或“ Continuum ”时才有效。但是其他系列有像“Family guy”这样的标题,所以当我创建 url 时,我使用了一个将其更改为“Family-guy”的函数,但由于某种原因,它不适用于带有“-”字符的标题。我总是收到这样的 404 错误

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.

所以这里的 url serialy/whats-with-andy/不匹配,但是如果我访问serialy/continuum它工作正常?有人对可能导致这种情况的原因有任何想法吗?哦,这就是视图的样子

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)
4

2 回答 2

0

正则表达式[\w]+仅匹配单词而不匹配特殊字符,例如-.

如果您将其更改为[-\w]+它将匹配“slug”网址。

于 2014-08-12T13:33:13.467 回答
0

我认为您的正则表达式失败,因为'-' 不被视为 \w 的匹配项。见这里: https ://docs.python.org/2/library/re.html

于 2014-08-12T13:36:06.923 回答