简体   繁体   English

Django - “模块”对象不可调用

[英]Django - 'module' object is not callable

I'm trying to serve images from the database with django, but when I add我正在尝试使用 django 从数据库中提供图像,但是当我添加

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

to my urlpatterns i get this error: TypeError at /: module object is not callable到我的 urlpatterns 我收到这个错误:TypeError at /: module object is not callable

this is the traceback:这是回溯:

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

I tried using it in both the main urls.py and the app urls.py, but it didn't work.我尝试在主 urls.py 和应用程序 urls.py 中使用它,但没有奏效。

my media_root and media_urls are the following:我的 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/'

i tried reimporting everything with我尝试重新导入所有内容

from django.conf.urls import *

but it didn't work either.但它也不起作用。

edit, these are my imports:编辑,这些是我的进口:

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

I'm using the last version of django and deploying in heroku (locally with heroku local)我正在使用最新版本的 django 并在 heroku 中部署(本地与本地 heroku)

Change改变

from django.conf.urls import static

to

from django.conf.urls.static import static

and you're good to go.你可以走了。

from django.conf.urls.static import static

写这一行,而不是

from django.conf.urls import static

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM