没有名为“ django.contrib.staticfiles.templatetags”的模块

时间:2019-12-10 06:15:51

标签: python django django-staticfiles django-settings

我已经为此整整一整天都无法解决,但无法找出问题所在。在我将Django项目从一台PC复制到另一台之后,发生了这种情况。

Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/Users/username/opt/anaconda3/lib/python3.7/threading.py", line 926, in _bootstrap_inner
    self.run()
  File "/Users/username/opt/anaconda3/lib/python3.7/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/username/opt/anaconda3/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "/Users/username/opt/anaconda3/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "/Users/username/opt/anaconda3/lib/python3.7/site-packages/django/utils/autoreload.py", line 76, in raise_last_exception
    raise _exception[1]
  File "/Users/username/opt/anaconda3/lib/python3.7/site-packages/django/core/management/__init__.py", line 357, in execute
    autoreload.check_errors(django.setup)()
  File "/Users/username/opt/anaconda3/lib/python3.7/site-packages/django/utils/autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "/Users/username/opt/anaconda3/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/username/opt/anaconda3/lib/python3.7/site-packages/django/apps/registry.py", line 122, in populate
    app_config.ready()
  File "/Users/username/opt/anaconda3/lib/python3.7/site-packages/django/contrib/admin/apps.py", line 24, in ready
    self.module.autodiscover()
  File "/Users/username/opt/anaconda3/lib/python3.7/site-packages/django/contrib/admin/__init__.py", line 26, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "/Users/username/opt/anaconda3/lib/python3.7/site-packages/django/utils/module_loading.py", line 47, in autodiscover_modules
    import_module('%s.%s' % (app_config.name, module_to_search))
  File "/Users/username/opt/anaconda3/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/saitohiromasa/opt/anaconda3/lib/python3.7/site-packages/django_summernote/admin.py", line 5, in <module>
    from django_summernote.widgets import SummernoteWidget, SummernoteInplaceWidget
  File "/Users/username/opt/anaconda3/lib/python3.7/site-packages/django_summernote/widgets.py", line 4, in <module>
    from django.contrib.staticfiles.templatetags.staticfiles import static
ModuleNotFoundError: No module named 'django.contrib.staticfiles.templatetags'

在settings.py中:

INSTALLED_APPS = [
    'django_summernote',
    'accounts.apps.AccountsConfig',
    'blogs.apps.BlogsConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

我的Django版本是3.0 这可能是什么问题?请帮帮我。非常感谢!

3 个答案:

答案 0 :(得分:20)

为了避免其他人最终解决django 3函数位置更改的问题,我将其保留在这里。

在django 3中,似乎静态模板标签在内置模板标签之间移动。

https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#std:templatetag-static

https://github.com/django/django/blob/50cf183d219face91822c75fa0a15fe2fe3cb32d/django/templatetags/static.py#L162

因此,您无需从这里导入它:from django.contrib.staticfiles.templatetags.staticfiles import static,而是需要从这里导入它:from django.templatetags.static import static

答案 1 :(得分:7)

在接下来的几年里,随着每个人都开始使用Django 3,这将是很普遍的事情。

除了接受的答案外,这也是我为支持Django 2和Django 3 static导入而添加的内容(特别是有助于管理软件包)

try:
    # Django 2
    from django.contrib.staticfiles.templatetags.staticfiles import static
except ModuleNotFoundError:
    # Django 3
    from django.templatetags.static import static

答案 2 :(得分:2)

django.contrib.staticfiles.templatetagsHere is the answer you need

  

已删除staticfiles和admin_static模板标记库。

django-summernote软件包自1月以来未更新,并且不支持Django 3