简体   繁体   English

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

[英]No module named 'django.contrib.staticfiles.templatetags'

I have been breaking my head over this for a full day but can't figure out the problem.我一整天都在思考这个问题,但无法弄清楚问题所在。 It happened after I copied my Django project from one PC to another.它发生在我将我的 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'

In settings.py:在 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',
]

My Django version is 3.0 What might be wrong here ?我的 Django 版本是 3.0 这里可能有什么问题? Please help me out.请帮帮我。 Much thanks!非常感谢!

I'll leave this here just in case other people end up in this question to fix django 3 function location change.我会把这个留在这里,以防其他人最终在这个问题中解决 django 3 功能位置更改。

It seems like in django 3, static templatetag is moved among builtin template tags.似乎在 django 3 中,静态模板标签在内置模板标签之间移动。

https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#std:templatetag-static 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 https://github.com/django/django/blob/50cf183d219face91822c75fa0a15fe2fe3cb32d/django/templatetags/static.py#L162

So instead of importing it from here: from django.contrib.staticfiles.templatetags.staticfiles import static , you need to import it from here: from django.templatetags.static import static所以不是从这里导入它: from django.contrib.staticfiles.templatetags.staticfiles import static ,你需要从这里导入它: from django.templatetags.static import static

This is going to be pretty common for a while as everyone starts to move into Django 3 over the next few years.这将在一段时间内非常普遍,因为在接下来的几年中每个人都开始进入 Django 3。

In addition to the accepted answer, this is what I've been adding to support both Django 2 and Django 3 static imports (esp. helpful with managing packages)除了已接受的答案之外,这是我一直在添加的内容以支持 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

django.contrib.staticfiles.templatetags was removed in version 3 django.contrib.staticfiles.templatetags 在版本 3 中删除

The staticfiles and admin_static template tag libraries are removed.删除了 staticfiles 和 admin_static 模板标记库。

The django-summernote package has not been updated since January and does not support Django 3 django-summernote包从一月开始没有更新,不支持Django 3

检查已安装应用程序列表中的逗号,一个逗号可能会毁了您的生活

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

相关问题 Django django.contrib.staticfiles.templatetags.static 已在 3.0 中删除:如何替换该功能? - Django django.contrib.staticfiles.templatetags.static removed in 3.0: How could I replace the functionality? &#39;django.contrib.staticfiles&#39;的问题, - Issues with 'django.contrib.staticfiles', 错误:没有名为staticfiles的模块 - Error: No module named staticfiles 没有名为 django.contrib.auth.hashers 的模块 - No module named django.contrib.auth.hashers ModuleNotFoundError:没有名为“django.contrib.staticfilesbase”的模块 - ModuleNotFoundError: No module named 'django.contrib.staticfilesbase' 带有Django的Pyinstaller:ModuleNotFoundError:没有名为django.contrib.messages.apps的模块 - Pyinstaller with Django: ModuleNotFoundError: No Module Named django.contrib.messages.apps 带有Django的Pyinstaller:ModuleNotFoundError:没有名为“ django.contrib.admin.apps”的模块 - Pyinstaller with Django: ModuleNotFoundError: No module named 'django.contrib.admin.apps' DJANGO CRUD 没有名为“django.contrib.name”的模块 - DJANGO CRUD No module named 'django.contrib.name' DJANGO:没有名为 social_auth.backends.contrib.github 的模块 - DJANGO: No module named social_auth.backends.contrib.github 错误:django 1.3.1 + python 2.6 中没有名为 staticfiles 的模块? 怎么了 - Error: No module named staticfiles in django 1.3.1 + python 2.6? what's wrong
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM