ahow
ahow

Reputation: 57

URL problem w/ code from a django-voting tutorial

I'm trying to use the django-voting tutorial from this blog:

http://new.justinlilly.com/blog/2008/nov/04/django-voting-a-brief-tutorial/

to get a simple up/down voting system working on an app of mine. But just like the first commenter from that post, this code in urls.py:

urlpatterns = patterns('',
 url(r'^(?P[-\w]+)/(?Pup|down|clear)vote/?$', vote_on_object, tip_dict, name="tip-voting"),
)

Gives me this error:

unknown specifier: ?P[

I'm terrible w/ regular expressions, anyone have an idea of how to fix that url?

Upvotes: 1

Views: 1405

Answers (1)

John Millikin
John Millikin

Reputation: 200846

Looks like his blog is mangling the URL. It should probably be:

url(r'^(?P<slug>[-\w]+)/(?P<direction>up|down|clear)vote/?$', vote_on_object, tip_dict, name="tip-voting"),

The pattern being used, from the Python docs, is a named group:

(?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>).

Upvotes: 3

Related Questions