TyperError Django在使用urls.py时

时间:2015-08-19 11:33:21

标签: django python-2.7 django-views django-urls

我是初学者,我正在使用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),
]

我做错了什么?

2 个答案:

答案 0 :(得分:5)

您不应该拥有patterns名称:

urlpatterns = [
    url(r'^$', views.domains),
]

答案 1 :(得分:2)

错误发生在 urls.py 中,您应该写:

urlpatterns = [
    url(r'^$', views.domains),
]

现在你正在使用方括号&#34; []&#34;在patterns之后 - 这是python解释器调用patterns.__getitem__()方法的信号,该方法不存在。这就是你收到错误的原因。