Django URL syntax error
I am creating a wiki app. And when I create url for my app in urls.py I get syntax error message. I am not good at regular expressions.
Here is my code,
(r'wikicamp/(?<page_name>[^/]+)/edit/$', 'wikicamp.wiki.views.edit_page'),
(r'wikicamp/(?<page_name>[^/]+)/save/$', 'wikicamp.wiki.views.save_page'),
(r'wikicamp/(?<page_name>[^/]+)/$', 'wikicamp.wiki.views.view_page'),
And my error is,
error at /
syntax error
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.2.5
Exception Type: error
Exception Value:
syntax error
Exception Location: /usr/lib/python2.6/re.py in _compile, line 245
Python开发者_如何学运维 Executable: /usr/bin/python
Python Version: 2.6.6
Python Path: ['/home/ztron/wikicamp', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/local/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/usr/lib/python2.6/dist-packages/gst-0.10', '/usr/lib/pymodules/python2.6', '/usr/lib/python2.6/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.6/gtk-2.0']
Server time: Wed, 13 Apr 2011 15:21:18 -0500
Is there anything wrong in the regular expression? I did read django documentation but still have no idea.
Thanks!
Your named capture groups are missing the 'P'. Do it like so:
(r'wikicamp/(?P<page_name>[^/]+)/edit/$', 'wikicamp.wiki.views.edit_page'),
(r'wikicamp/(?P<page_name>[^/]+)/save/$', 'wikicamp.wiki.views.save_page'),
(r'wikicamp/(?P<page_name>[^/]+)/$', 'wikicamp.wiki.views.view_page'),
According to http://docs.python.org/library/re.html the syntax for named groups is
(?P<name>...)
That means you should have
(r'wikicamp/(?P<page_name>[^/]+)/edit/$', 'wikicamp.wiki.views.edit_page'),
精彩评论