2009-08-18 31 views
1

我試圖使用Django的投票教程從這個博客/代碼:URL問題瓦特從Django的投票教程

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[ 

我是可怕的W /正則表達式,人有一個想法如何修復該網址?

回答

3

看起來他的博客正在改變URL。它可能應該是:

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 

is accessible within the rest of the regular expression via the symbolic group name name. Group names must be valid Python identifiers, and each group name must be defined only once within a regular expression. A symbolic group is also a numbered group, just as if the group were not named. So the group named id in the example below can also be referenced as the numbered group 1.

For example, if the pattern is `(?P<id>[a-zA-Z_]\w*)`, the group can be 

referenced by its name in arguments to methods of match objects, such as m.group('id') or m.end('id') , and also by name in the regular expression itself (using (?P=id)) and replacement text given to .sub() (using \g<id>).

+0

啊,你說得對,我應該已經注意到了。非常感謝,謝謝。 – ahow 2009-08-18 21:59:33