Django URL语法错误

时间:2011-04-13 20:24:06

标签: python regex django django-urls

我正在创建一个wiki应用程序。当我在urls.py中为我的应用程序创建url时,我收到语法错误消息。我不擅长正则表达式。

这是我的代码,

(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'),

我的错误是,

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

正则表达式有什么问题吗?我读过django文档,但仍然不知道。

谢谢!

2 个答案:

答案 0 :(得分:4)

您的命名捕获组缺少“P”。这样做:

(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'),

答案 1 :(得分:0)

根据http://docs.python.org/library/re.html,命名组的语法是

(?P<name>...)

这意味着你应该

(r'wikicamp/(?P<page_name>[^/]+)/edit/$', 'wikicamp.wiki.views.edit_page'),