django 错误无法导入名称'RemovedInDjango30Warning'

新手上路,请多包涵

伙计们我对 Django 还很陌生,我刚开始从事个人项目并决定滥用 pycharm(我认为它是否与错误有关)。

当我运行 python manage.py runserver 我收到下面发布的错误。我做了一些谷歌搜索,看起来它是由与 Django 版本不一致引起的。

我目前有 Django 3.0,我在全局和 venv 中都进行了检查。

我试图在 py charm 之外启动一个项目,但我遇到了同样的问题。我知道我需要做什么才能再次开始使用 Django。有人遇到过这个吗?这是因为pycharm吗?如果是这样,我该怎么做才能解决这个问题?

 (venv) aiden@aiden-XPS-15-9570:~/PycharmProjects/NewsAggregator$ python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/home/aiden/.local/lib/python3.6/site-packages/django/template/utils.py", line 66, in __getitem__
    return self._engines[alias]
KeyError: 'django'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/aiden/.local/lib/python3.6/site-packages/django/template/backends/django.py", line 121, in get_package_libraries
    module = import_module(entry[1])
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/aiden/.local/lib/python3.6/site-packages/django/contrib/admin/templatetags/admin_static.py", line 5, in <module>
    from django.utils.deprecation import RemovedInDjango30Warning
ImportError: cannot import name 'RemovedInDjango30Warning'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "/home/aiden/.local/lib/python3.6/site-packages/django/utils/autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "/home/aiden/.local/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
    self.check(display_num_errors=True)
  File "/home/aiden/.local/lib/python3.6/site-packages/django/core/management/base.py", line 395, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/aiden/.local/lib/python3.6/site-packages/django/core/management/base.py", line 382, in _run_checks
    return checks.run_checks(**kwargs)
  File "/home/aiden/.local/lib/python3.6/site-packages/django/core/checks/registry.py", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/aiden/.local/lib/python3.6/site-packages/django/contrib/admin/checks.py", line 76, in check_dependencies
    for engine in engines.all():
  File "/home/aiden/.local/lib/python3.6/site-packages/django/template/utils.py", line 90, in all
    return [self[alias] for alias in self]
  File "/home/aiden/.local/lib/python3.6/site-packages/django/template/utils.py", line 90, in <listcomp>
    return [self[alias] for alias in self]
  File "/home/aiden/.local/lib/python3.6/site-packages/django/template/utils.py", line 81, in __getitem__
    engine = engine_cls(params)
  File "/home/aiden/.local/lib/python3.6/site-packages/django/template/backends/django.py", line 25, in __init__
    options['libraries'] = self.get_templatetag_libraries(libraries)
  File "/home/aiden/.local/lib/python3.6/site-packages/django/template/backends/django.py", line 43, in get_templatetag_libraries
    libraries = get_installed_libraries()
  File "/home/aiden/.local/lib/python3.6/site-packages/django/template/backends/django.py", line 108, in get_installed_libraries
    for name in get_package_libraries(pkg):
  File "/home/aiden/.local/lib/python3.6/site-packages/django/template/backends/django.py", line 125, in get_package_libraries
    "trying to load '%s': %s" % (entry[1], e)
django.template.library.InvalidTemplateLibrary: Invalid template library specified. ImportError raised when trying to load 'django.contrib.admin.templatetags.admin_static': cannot import name 'RemovedInDjango30Warning'

原文由 Aiden Campbell 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.4k
2 个回答

注释掉以下行:

 from django.utils.deprecation import RemovedInDjango30Warning

在以下文件中:

 python3.6/site-packages/django/contrib/admin/templatetags/admin_static.py
python3.6/site-packages/django/contrib/staticfiles/templatetags/staticfiles.py

原文由 y durga prasad 发布,翻译遵循 CC BY-SA 4.0 许可协议

这是由 Django 版本冲突引起的,如此处所示。

导入错误:无法导入名称“RemovedInDjango30Warning”

尝试卸载 django

 sudo pip uninstall django

并使用低于 3.0 的版本重新安装

sudo pip install django==2.2

编辑

如果你想使用不同版本的 Django,你可以使用虚拟环境。

首先从 这里 创建一个 requirements.txt 示例

您可以通过运行 pip freeze 命令来生成项目的要求,该命令列出了本地机器上安装的所有包及其版本。

 pip freeze > requirements.txt

请注意,此过程可能导致某些不必要的包被写入需求文件,这些包已安装在您的本地计算机上,但项目不需要。在这种情况下,您必须手动编辑需求文件。

然后创建你的虚拟环境

第一步 安装虚拟环境

pip install virtualenv

Step 2 创建虚拟环境

virtualenv env

第 3 步激活您的环境

env\Scripts\activate

当你想停用

deactivate

第 4 步编辑您的 requirements.txt 以获得您的项目所需的包。

第 5 步安装 requirements.txt(在同一个目录)

 pip install -r requirements.txt

有关使用 mod_wsgi 和 Apache 进行部署的表单信息,请在 此处 尝试

原文由 recurseuntilfor 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题