TypeError:'function'对象没有属性'__getitem__'; MEDIA_URL

时间:2016-04-04 01:59:46

标签: python django django-settings django-1.9

我正在尝试在django中提供媒体服务,并遵循文档建议:

的index.html:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Sitename</title>

        <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.css" rel="stylesheet">
        <script src="/media/js/codemirror/codemirror.js"></script>
        <script src="/media/js/codemirror/codemirror-modes.js"></script>
        <link rel="stylesheet" href="/media/css/codemirror.css">
    </head>

settings.py:

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/

STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

主要的urls.py:

from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.conf import settings
from django.contrib import admin

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

    url(r'^admin/', include(admin.site.urls)),
    url(r'^', include('index.urls', namespace='index')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

enter image description here

我收到错误:

Unhandled exception in thread started by <function wrapper at 0x7febccf77668>
Traceback (most recent call last):
  File "/home/cchilders/.virtualenvs/scriptamajig/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/home/cchilders/.virtualenvs/scriptamajig/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 116, in inner_run
    self.check(display_num_errors=True)
  File "/home/cchilders/.virtualenvs/scriptamajig/local/lib/python2.7/site-packages/django/core/management/base.py", line 426, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/cchilders/.virtualenvs/scriptamajig/local/lib/python2.7/site-packages/django/core/checks/registry.py", line 75, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/cchilders/.virtualenvs/scriptamajig/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "/home/cchilders/.virtualenvs/scriptamajig/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver
    for pattern in resolver.url_patterns:
  File "/home/cchilders/.virtualenvs/scriptamajig/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/cchilders/.virtualenvs/scriptamajig/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 417, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/cchilders/.virtualenvs/scriptamajig/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/cchilders/.virtualenvs/scriptamajig/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/cchilders/projects/scriptamajig/scriptamajig/urls.py", line 12, in <module>
    url(r'^', include('index.urls', namespace='index')),
TypeError: 'function' object has no attribute '__getitem__'

这完全按照服务媒体的文档中所述:

https://docs.djangoproject.com/en/1.9/howto/static-files/#serving-uploaded-files-in-development

为什么这些文件无法加载?谢谢

尝试像:

from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.conf import settings
from django.contrib import admin

urlpatterns = patterns[
    url(r'^admin/', include(admin.site.urls)),
    url(r'^', include('index.urls', namespace='index')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

产生相同的错误

Unhandled exception in thread started by <function wrapper at 0x7f69877f5668>
Traceback (most recent call last):
  File "/home/cchilders/.virtualenvs/scriptamajig/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/home/cchilders/.virtualenvs/scriptamajig/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 116, in inner_run
    self.check(display_num_errors=True)
  File "/home/cchilders/.virtualenvs/scriptamajig/local/lib/python2.7/site-packages/django/core/management/base.py", line 426, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/cchilders/.virtualenvs/scriptamajig/local/lib/python2.7/site-packages/django/core/checks/registry.py", line 75, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/cchilders/.virtualenvs/scriptamajig/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "/home/cchilders/.virtualenvs/scriptamajig/local/lib/python2.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver
    for pattern in resolver.url_patterns:
  File "/home/cchilders/.virtualenvs/scriptamajig/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/cchilders/.virtualenvs/scriptamajig/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 417, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/cchilders/.virtualenvs/scriptamajig/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/cchilders/.virtualenvs/scriptamajig/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/cchilders/projects/scriptamajig/scriptamajig/urls.py", line 8, in <module>
    url(r'^', include('index.urls', namespace='index')),
TypeError: 'function' object has no attribute '__getitem__'

scriptamajig /索引/ urls.py:

from django.conf.urls import patterns, include, url

from index import views

urlpatterns = patterns('',            
    url(r'^$',          views.index,             name='index'),
)

1 个答案:

答案 0 :(得分:2)

问题在于:

urlpatterns = patterns['', # <- see how you are trying to access patterns

相反,只需将urlpatterns定义为列表:

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^', include('index.urls', namespace='index')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)