2012-07-26 46 views
3
from django.conf.urls import patterns, include, url 
from django.contrib import admin 

admin.autodiscover() 

urlpatterns = patterns('', 
    (r'^admin/', include(admin.site.urls)), 
    (r'^events/', include('events.urls')), 
) 

這裏是我events.urls:Django的:在urls.py語法錯誤

from django.conf.urls.defaults import * 
from events import views 


urlpatterns = patterns('', 
    url(r'^tonight/$', views.tonight, name='ev_tonight'), 
) 

我運行服務器後收到以下錯誤:

 
Exception Type:  SyntaxError 
Exception Value:

invalid syntax (urls.py, line 8)

上午我在這裏錯過了什麼?

編輯:連接traceball

環境:

Request Method: GET 
Request URL: http://127.0.0.1:8000/admin 

Django Version: 1.4 
Python Version: 2.7.3 
Installed Applications: 
('django.contrib.auth', 
'django.contrib.contenttypes', 
'django.contrib.sessions', 
'django.contrib.sites', 
'django.contrib.messages', 
'django.contrib.staticfiles', 
'django.contrib.admin', 
'events') 
Installed Middleware: 
('django.middleware.common.CommonMiddleware', 
'django.contrib.sessions.middleware.SessionMiddleware', 
'django.middleware.csrf.CsrfViewMiddleware', 
'django.contrib.auth.middleware.AuthenticationMiddleware', 
'django.contrib.messages.middleware.MessageMiddleware') 


Traceback: 
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response 
    89.      response = middleware_method(request) 
File "/usr/local/lib/python2.7/dist-packages/django/middleware/common.py" in process_request 
    67.    if (not urlresolvers.is_valid_path(request.path_info, urlconf) and 
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in is_valid_path 
    531.   resolve(path, urlconf) 
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve 
    420.  return get_resolver(urlconf).resolve(path) 
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve 
    298.    for pattern in self.url_patterns: 
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in url_patterns 
    328.   patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) 
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in urlconf_module 
    323.    self._urlconf_module = import_module(self.urlconf_name) 
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py" in import_module 
    35.  __import__(name) 
File "/home/bhavish/startthedark/startthedark/urls.py" in <module> 
    8.  (r'^events/', include('events.urls')), 
File "/usr/local/lib/python2.7/dist-packages/django/conf/urls/__init__.py" in include 
    24.   urlconf_module = import_module(urlconf_module) 
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py" in import_module 
    35.  __import__(name) 

Exception Type: SyntaxError at /admin 
Exception Value: invalid syntax (urls.py, line 8 

+1

代碼沒有問題。檢查代碼文件中的一些特殊符號...或檢查events.urls的錯誤... – Tisho 2012-07-26 12:28:49

+0

猜你可以刪除引用events.urls – Pramod 2012-07-26 12:54:35

+0

@Pramod,不,你不能,除非你專門導入事件模塊。 – 2012-07-26 13:50:04

回答

8

的在events/urls.py中引發錯誤 - 仔細檢查該文件。在它的結尾處可能會有一些額外的空白區域,您看不到它們。 (語法錯誤是假設在第8行,但我沒有看到你粘貼的8行)

你也可以嘗試直接從python shell中導入該文件,看它是否解析。

+2

感謝您提供「直接從python shell導入該文件」的建議。有效! exta'〜'複製urls.py – futurenext110 2012-07-26 14:24:26

+0

的內容謝謝pro-tip嘗試使用python shell。立即找到它! – Fydo 2015-03-26 20:22:07

+0

極好的解決方案http://stackoverflow.com/users/70605/ian-clelland – user956424 2015-04-16 07:18:53

0

也許這一行: (r'^admin/', include(admin.site.urls)), 應改爲(加括號): (r'^admin/', include('admin.site.urls')),

+0

這會在運行時導致一個'NameError',而不是導入時的'SyntaxError'。 – 2012-07-26 14:03:31