Django在导入urls.py时出现语法错误

时间:2013-10-19 19:56:47

标签: django django-forms django-urls django-authentication django-errors

我的testProject / urls.py就是这个

from django.conf.urls import patterns, include, url
from testapp import urls
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

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

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
     url(r'^admin/', include(admin.site.urls)),
     url(r'^', include(urls)),
)

我的testApp / urls.py就是这个

from django.conf.urls import patterns, include, url
from testapp.forms import UsersForm
from templates import login.html

urlpatterns = patterns('',

url(r'^$', 'django.contrib.auth.views.login', {'template_name': 'testproject/templates/login.html', 'authentication_form':UsersForm}),

现在,当我通过执行

运行服务器时
python manage.py runserver

它给了我一个

SyntaxError at /

错误说

invalid syntax (urls.py, line 3)
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 1.5.2
Exception Type: SyntaxError
Exception Value:    
invalid syntax (urls.py, line 3)
Exception Location: /home/ayman/Documents/djcode/testproject/testproject/urls.py in   <module>, line 2

并且追溯是

Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  103.                     resolver_match = resolver.resolve(request.path_info)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve
  319.             for pattern in self.url_patterns:
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in url_patterns
  347.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in urlconf_module
  342.             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/ayman/Documents/djcode/testproject/testproject/urls.py" in <module>
  2. from testapp import urls

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

知道它为什么会引发语法错误?这是重量,因为有些地方它说错误在第3行,而其他地方说它在第2行。请注意,这只是在几分钟前工作,直到我决定更改并使用Django的通用登录视图。我有一个错误将表单作为authentication_form传递,我修复了该错误,但在我修复该错误之后,引发了这个语法错误。

关于将表单作为authentication_form传递的上一个问题可以在此处查看作为参考

Django generic login view return 'str object not callable' error

只是帮助它。

3 个答案:

答案 0 :(得分:2)

testapp.urls中有错误

from django.conf.urls import patterns, include, url
from testapp.forms import UsersForm
from templates import login.html

urlpatterns = patterns('',

url(r'^$', 'django.contrib.auth.views.login', {'template_name': 'testproject/templates/login.html', 'authentication_form':UsersForm}),
)

from templates import login.html不是你应该导入的东西,因为它不是python源码。你可以删除这一行。因为视图只需要一个字符串作为template_name的参数,并且不需要Python对象。

答案 1 :(得分:1)

请在testProject / urls.py中尝试以下内容:

from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()

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

另一种方法:

from django.conf.urls import patterns, include, url
from testapp.urls import urlpatterns as testapp_urls
from django.contrib import admin
admin.autodiscover()

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

另请阅读文档的以下部分,内容涉及include
https://docs.djangoproject.com/en/dev/topics/http/urls/#including-other-urlconfs

答案 2 :(得分:0)

esta es la solucion a tu problema solo debes cerrar entre comillas'admin.site.urls'y listo。

#取消注释下一行以启用管理员:      url(r'^ admin /',include('admin.site.urls')),      url(r'^',include(urls)),