Django - '模块'对象不可调用

时间:2018-01-20 20:29:39

标签: python django heroku

我尝试使用django从数据库中提供图片,但是当我添加

+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

到我的urlpatterns我收到此错误:/:模块对象的TypeError不可调用

这是追溯:

File "/home/watch/Documents/projects/herokuapp/venv/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  35.             response = get_response(request)

File "/home/watch/Documents/projects/herokuapp/venv/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  113.         resolver_match = resolver.resolve(request.path_info)

File "/home/watch/Documents/projects/herokuapp/venv/lib/python3.6/site-packages/django/urls/resolvers.py" in resolve
  494.             for pattern in self.url_patterns:

File "/home/watch/Documents/projects/herokuapp/venv/lib/python3.6/site-packages/django/utils/functional.py" in __get__
  36.         res = instance.__dict__[self.name] = self.func(instance)

File "/home/watch/Documents/projects/herokuapp/venv/lib/python3.6/site-packages/django/urls/resolvers.py" in url_patterns
  536.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)

File "/home/watch/Documents/projects/herokuapp/venv/lib/python3.6/site-packages/django/utils/functional.py" in __get__
  36.         res = instance.__dict__[self.name] = self.func(instance)

File "/home/watch/Documents/projects/herokuapp/venv/lib/python3.6/site-packages/django/urls/resolvers.py" in urlconf_module
  529.             return import_module(self.urlconf_name)

File "/usr/lib/python3.6/importlib/__init__.py" in import_module
  126.     return _bootstrap._gcd_import(name[level:], package, level)

File "<frozen importlib._bootstrap>" in _gcd_import
  994. <source code not available>

File "<frozen importlib._bootstrap>" in _find_and_load
  971. <source code not available>

File "<frozen importlib._bootstrap>" in _find_and_load_unlocked
  955. <source code not available>

File "<frozen importlib._bootstrap>" in _load_unlocked
  665. <source code not available>

File "<frozen importlib._bootstrap_external>" in exec_module
  678. <source code not available>

File "<frozen importlib._bootstrap>" in _call_with_frames_removed
  219. <source code not available>

File "/home/watch/Documents/projects/herokuapp/mysite/mysite/urls.py" in <module>
  27. ]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Exception Type: TypeError at /
Exception Value: 'module' object is not callable

我尝试在主urls.py和app urls.py中使用它,但它没有用。

我的media_root和media_urls如下:

abspath = lambda *p: os.path.abspath(os.path.join(*p))

PROJECT_ROOT = abspath(os.path.dirname(__file__))

MEDIA_ROOT = abspath(PROJECT_ROOT, 'media')
MEDIA_URL = '/media/'

我尝试用

重新导入所有内容
from django.conf.urls import *

但它也没有用。

编辑,这些是我的导入:

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

我使用的是最新版本的django并在heroku中部署(本地使用heroku本地)

2 个答案:

答案 0 :(得分:4)

更改

from django.conf.urls import static

from django.conf.urls.static import static

你很高兴。

答案 1 :(得分:-1)

from django.conf.urls.static import static

写这一行,而不是

from django.conf.urls import static