我有一个奇怪的问题不能弄清楚什么是错的,我得到404错误的任何url,包含'-'字符…
我的url .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/what -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)
正则表达式[w]+
只匹配单词,不匹配特殊字符,如-
。
如果您将其更改为[-w]+
,它将匹配"slug" url
我认为你的正则表达式失败了,因为'-'不被认为是w的匹配。在这里看到的:https://docs.python.org/2/library/re.html