1

我的 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 传递时出错,我修复了该错误,但在修复该错误之后,引发了此语法错误。

之前关于将Form作为authentication_form传递的问题可以看这里作为参考

Django 通用登录视图返回“str object not callable”错误

把它包起来有帮助。

4

2 回答 2

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 对象。

于 2013-10-19T20:28:16.843 回答
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/#include-other-urlconfs

于 2013-10-19T20:07:56.080 回答