2016-04-04 166 views
1

我想成爲在Django媒體和我按照文檔建議:TypeError:'function'對象沒有屬性'__getitem__'; MEDIA_URL

的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

的問題是在這裏:

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) 
+0

沒有工作 – codyc4321

+1

@ codyc4321你是什麼意思沒有工作?你重新啓動了服務器嗎?謝謝。 – alecxe

+0

我現在編輯上面,服務器重新啓動 – codyc4321

相關問題