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

Posted

技术标签:

【中文标题】Django 在导入 urls.py 时出现语法错误【英文标题】:Django giving syntax error when importing urls.py 【发布时间】:2013-10-28 12:35:59 【问题描述】:

我的 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 generic login view return 'str object not callable' error

把它包起来会有帮助。

【问题讨论】:

【参考方案1】:

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

【讨论】:

【参考方案2】:

请在 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

【讨论】:

奇怪,它现在给我一个 NameError 说“名称'testapp'未定义”在“url(r'^',include('testapp.urls'))”行.. 我试着让它 testproject.testapp.urls 然后它给出了一个名称错误,说“名称'testproject'没有定义”。我的项目称为 testproject,应用程序称为 testapp。我应该有一个 import 语句导入 testproject 吗? 您是否在 settings.py 的 INSTALLED_APPS 中包含了 testapp? 是的,在我的 settings.py 中,我的 INSTALLED_APPS 下确实有 testapp。 奇怪,它应该是这样工作的。您似乎忘记了“testapp.urls”中的引号。无论如何,我在答案中添加了另一种方法 - 请尝试。 hm 很奇怪,我尝试了新方法,现在它在“from testapp.urls import urlpatterns as testapp_urls”这一行给了我一个语法错误

以上是关于Django 在导入 urls.py 时出现语法错误的主要内容,如果未能解决你的问题,请参考以下文章

使用通用登录视图时出现“名称'django'未定义”错误?

使用 Django,有没有更好的方法在 urls.py 中动态导入视图?

我是 Django 的初学者,在 urls.py 中遇到语法错误

DJango 中 URLS.py 中的无效语法错误

升级到Django 2.1的urls.py路径中的语法错误

Django:ImportError at / No module named urls