Django中urls.py中的语法错误

时间:2012-08-31 13:58:54

标签: python

我正在尝试在urls.py中添加一些网址。

urls.py

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'Permissions.views.home', name='home'),
    # url(r'^Permissions/', include('Permissions.foo.urls')),
     #url(r'^Permissions$', include('Permissions.Authenication.urls')),
     url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
     url(r'^admin/', include(admin.site.urls)),
     url(r'^$',RedirectView.as_view(url='/welcome/'),
)

urlpatterns += patterns('',
     url(r'^register/$', 'drinker.views.DrinkerRegistration',name='DrinkerRegistration'),
     url(r'^welcome/$','drinker.views.DrinkerWelcome',name='DrinkerWelcome'),
     url(r'^home/(?P<userpk>[^/]+)/$','drinker.views.DrinkerHome',  name='DrinkerHome'),     
)

错误:

Traceback:
File "/Users/cnnlakshmen_2000/Projects/env/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  101.                             request.path_info)
File "/Users/cnnlakshmen_2000/Projects/env/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
  298.             for pattern in self.url_patterns:
File "/Users/cnnlakshmen_2000/Projects/env/lib/python2.7/site-packages/django/core/urlresolvers.py" in url_patterns
  328.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/cnnlakshmen_2000/Projects/env/lib/python2.7/site-packages/django/core/urlresolvers.py" in urlconf_module
  323.             self._urlconf_module = import_module(self.urlconf_name)
File "/Users/cnnlakshmen_2000/Projects/env/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)

Exception Type: SyntaxError at /
Exception Value: invalid syntax (urls.py, line 17)

不确定错误在哪里......需要一些帮助...

1 个答案:

答案 0 :(得分:6)

你错过了一个括号并有一个额外的逗号:

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'Permissions.views.home', name='home'),
    # url(r'^Permissions/', include('Permissions.foo.urls')),
    # url(r'^Permissions$', include('Permissions.Authenication.urls')),

    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', RedirectView.as_view(url='/welcome/')) #Error was here
)

你的下半部分还有另一个逗号:

urlpatterns += patterns('',
    url(r'^register/$', 'drinker.views.DrinkerRegistration',name='DrinkerRegistration'),
    url(r'^welcome/$','drinker.views.DrinkerWelcome',name='DrinkerWelcome'),
    url(r'^home/(?P<userpk>[^/]+)/$','drinker.views.DrinkerHome', name='DrinkerHome') # Comma was here     
)