我正在尝试使用此博客中的django-voting教程:
http://new.justinlilly.com/blog/2008/nov/04/django-voting-a-brief-tutorial/
获得一个简单的上/下投票系统在我的应用程序上工作。但就像该帖子中的第一个评论者一样,这个代码在urls.py中:
urlpatterns = patterns('',
url(r'^(?P[-\w]+)/(?Pup|down|clear)vote/?$', vote_on_object, tip_dict, name="tip-voting"),
)
给我这个错误:
unknown specifier: ?P[
我对正则表达式很糟糕,任何人都知道如何修复该网址?
答案 0 :(得分:3)
看起来他的博客正在破坏网址。应该是:
url(r'^(?P<slug>[-\w]+)/(?P<direction>up|down|clear)vote/?$', vote_on_object, tip_dict, name="tip-voting"),
Python docs中使用的模式是一个命名组:
(?P<name>...)
Similar to regular parentheses, but the substring matched by the group
可在其余部分内访问 通过象征性的正则表达式 组名称。组名必须是 有效的Python标识符,以及每个 组名必须只定义一次 在正则表达式中。一个 象征性群体也是一个编号 小组,就好像小组没有 命名。所以该组名为id 以下示例也可以参考 作为编号组1。
For example, if the pattern is `(?P<id>[a-zA-Z_]\w*)`, the group can be
在其参数中引用其名称 匹配对象的方法,例如
m.group('id')
或m.end('id')
,以及 在正则表达式中按名称 本身(使用(?P=id)
)和替换 提供给.sub()
的文字(使用\g<id>
)。