使用 urls.py 时出现 TyperError Django
Posted
技术标签:
【中文标题】使用 urls.py 时出现 TyperError Django【英文标题】:TyperError Django while using urls.py 【发布时间】:2015-11-12 16:25:16 【问题描述】:我是初学者,我正在使用 Django 1.8.3 和 Python 2.7.3。 我正在尝试运行服务器并收到以下错误:
Traceback:
File "/home/mercurial/internet.django/djangoenv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
108. response = middleware_method(request)
File "/home/mercurial/internet.django/djangoenv/local/lib/python2.7/site-packages/django/middleware/common.py" in process_request
74. if (not urlresolvers.is_valid_path(request.path_info, urlconf) and
File "/home/mercurial/internet.django/djangoenv/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in is_valid_path
647. resolve(path, urlconf)
File "/home/mercurial/internet.django/djangoenv/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
522. return get_resolver(urlconf).resolve(path)
File "/home/mercurial/internet.django/djangoenv/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
366. for pattern in self.url_patterns:
File "/home/mercurial/internet.django/djangoenv/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in url_patterns
402. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/mercurial/internet.django/djangoenv/local/lib/python2.7/site-packages/django/core/urlresolvers.py" in urlconf_module
396. self._urlconf_module = import_module(self.urlconf_name)
File "/usr/lib/python2.7/importlib/__init__.py" in import_module
37. __import__(name)
File "/home/mercurial/internet.django/bzi/bzi/urls.py" in <module>
24. url(r'^domains/', include('mailserver.urls')),
File "/home/stas/mercurial/internet.django/djangoenv/local/lib/python2.7/site-packages/django/conf/urls/__init__.py" in include
33. urlconf_module = import_module(urlconf_module)
File "/usr/lib/python2.7/importlib/__init__.py" in import_module
37. __import__(name)
File "/home/mercurial/internet.django/bzi/mailserver/urls.py" in <module>
5. url(r'^$', views.domains, name='domains'),
Exception Type: TypeError at /domains
Exception Value: 'function' object has no attribute '__getitem__'
我的项目文件urls.py:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^domains/', include('mailserver.urls')),
]
我的应用邮件服务器文件 urls.py:
from django.conf.urls import patterns, url, include
from . import views
urlpatterns = patterns[
url(r'^$', views.domains),
]
我做错了什么?
【问题讨论】:
【参考方案1】:您不应该有 patterns
名称:
urlpatterns = [
url(r'^$', views.domains),
]
【讨论】:
很想知道为什么有人不赞成这个。patterns()
是 deprecated in Django 1.8,所以这是正确答案。
反正这个语法更优雅。
更正确。您不应该在新项目中使用已弃用的代码。【参考方案2】:
错误在urls.py中,你应该写:
urlpatterns = [
url(r'^$', views.domains),
]
现在您在patterns
之后使用方括号“[]” - 这是python 解释器调用patterns.__getitem__()
方法的信号,这是不存在的。这就是你得到错误的原因。
【讨论】:
非常感谢!真的行。我很困惑,我没有注意到,我将“模式”称为列表。以上是关于使用 urls.py 时出现 TyperError Django的主要内容,如果未能解决你的问题,请参考以下文章
为啥即使我设置了 STATIC_ROOT 和 urls.py 也会出现 404 错误