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

No module named 'django.contrib.staticfiles.templatetags'

我已经为这个问题苦恼了一整天,但无法找出问题所在。它发生在我将我的 Django 项目从一台 PC 复制到另一台 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 这里可能有什么问题?请帮帮我。非常感谢!

django.contrib.staticfiles.templatetagsremoved in version 3

The staticfiles and admin_static template tag libraries are removed.

django-summernote包从1月份开始就没有更新过,不支持Django 3

我会把它留在这里,以防其他人最终遇到这个问题来修复 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

在接下来的几年里,随着每个人都开始迁移到 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

检查已安装应用列表中的逗号 一个逗号可以毁掉你的生活