Django 2.1和Python 3.7:运行Apache2 mod_wsgi时出现KeyError:'django'和其他问题

时间:2018-08-14 16:28:20

标签: python django apache2 mod-wsgi

这么多小时后,我来找你,试图弄清楚为什么我的django设置不再起作用了。我向你想要的任何上帝发誓,昨晚我因此而没有入睡。

我想从Python 3.5切换到Python 3.7,因为我开始使用 f-strings ,并且由于Python 3.6提供了此功能,所以我想为什么不直接使用Python 3.7。另外,我希望我的2个域(mywebsite.fr和mywebsite.com)指向同一个django项目。到现在为止,只有mywebsite.fr才没有问题。

我将尽力向您详细解释为获得下面的代码而做的事情:

  • 我的安装在Ubuntu 16.04上
  • 我对本地安装在Ubuntu安装程序中的Python版本有些混乱。我终于成功安装了(我认为)Python 3.7,
  • 我真的很难配置/制作/安装适当的mod_wsgi,
  • 我将venv用于我的django设置(以下称为 m2gvenvprod ),
  • 我正在使用mod_wgsi及其关联的守护程序模式,
  • 使用我阅读的所有内容,我确信我在apache2中针对每个网站/域的conf是正确的(在可用站点中),
  • 在这里,我尝试了在互联网上发现的有关我的问题的所有内容后,使用下面的apache2 error.log。我无法理解该日志。我不明白为什么django不是有效的密钥。我暂时一无所知...真是绝望。

可能有一件有趣的事我注意到了:如果我访问mywebsite.com:错误500。但是如果我访问mywebsite.com/admin,我将被重定向到mywebsite.com/admin/login/?next=/admin/ :所以,必须有些东西还在工作,嗯?

Traceback (most recent call last):
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/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/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/backends/django.py", line 121, in get_package_libraries
    module = import_module(entry[1])
  File "/usr/local/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 "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django_libs/templatetags/libs_tags.py", line 6, in <module>
    from django.template.base import TOKEN_BLOCK, TOKEN_VAR
ImportError: cannot import name 'TOKEN_BLOCK' from 'django.template.base' (/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/base.py)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/core/handlers/base.py", line 156, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/core/handlers/base.py", line 154, in _get_response
    response = response.render()
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/response.py", line 106, in render
    self.content = self.rendered_content
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/response.py", line 81, in rendered_content
    template = self.resolve_template(self.template_name)
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/response.py", line 63, in resolve_template
    return select_template(template, using=self.using)
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/loader.py", line 38, in select_template
    engines = _engine_list(using)
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/loader.py", line 66, in _engine_list
    return engines.all() if using is None else [engines[using]]
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/utils.py", line 90, in all
    return [self[alias] for alias in self]
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/utils.py", line 90, in <listcomp>
    return [self[alias] for alias in self]
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/utils.py", line 81, in __getitem__
    engine = engine_cls(params)
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/backends/django.py", line 25, in __init__
    options['libraries'] = self.get_templatetag_libraries(libraries)
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/backends/django.py", line 43, in get_templatetag_libraries
    libraries = get_installed_libraries()
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/backends/django.py", line 108, in get_installed_libraries
    for name in get_package_libraries(pkg):
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/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_libs.templatetags.libs_tags': cannot import name 'TOKEN_BLOCK' from 'django.template.base' (/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/base.py)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/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/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/backends/django.py", line 121, in get_package_libraries
    module = import_module(entry[1])
  File "/usr/local/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 "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django_libs/templatetags/libs_tags.py", line 6, in <module>
    from django.template.base import TOKEN_BLOCK, TOKEN_VAR
ImportError: cannot import name 'TOKEN_BLOCK' from 'django.template.base' (/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/base.py)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/utils/deprecation.py", line 91, in __call__
    response = response or self.get_response(request)
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/core/handlers/exception.py", line 36, in inner
    response = response_for_exception(request, exc)
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/core/handlers/exception.py", line 90, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/core/handlers/exception.py", line 129, in handle_uncaught_exception
    return callback(request, **param_dict)
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/views/defaults.py", line 69, in server_error
    template = loader.get_template(template_name)
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/loader.py", line 12, in get_template
    engines = _engine_list(using)
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/loader.py", line 66, in _engine_list
    return engines.all() if using is None else [engines[using]]
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/utils.py", line 90, in all
    return [self[alias] for alias in self]
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/utils.py", line 90, in <listcomp>
    return [self[alias] for alias in self]
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/utils.py", line 81, in __getitem__
    engine = engine_cls(params)
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/backends/django.py", line 25, in __init__
    options['libraries'] = self.get_templatetag_libraries(libraries)
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/backends/django.py", line 43, in get_templatetag_libraries
    libraries = get_installed_libraries()
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/backends/django.py", line 108, in get_installed_libraries
    for name in get_package_libraries(pkg):
  File "/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/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_libs.templatetags.libs_tags': cannot import name 'TOKEN_BLOCK' from 'django.template.base' (/home/krazymax/django/M2Gproject/m2gvenvprod/lib/python3.7/site-packages/django/template/base.py)

提前感谢您为我提供的建议!


编辑1

现在它根本不起作用。我试图使其像以前一样在Python 3.5上运行,但没有成功。

2 个答案:

答案 0 :(得分:1)

在将apachepython 3.7.0.一起使用

幸运的是,django 2.1.4python 3.6.6.组合时可以正常工作

答案 1 :(得分:0)

好吧,我从没想过Django(2.1)的正式版本会成为问题,但是我们来了!在python 3.7下创建新的虚拟环境时,我没有立即注意django的新版本。即使在此之后,我都认为它是一个稳定的版本,仅此而已。因此,在我的Ubuntu安装上也许出了点问题(但实际上我看不到什么),但是从django 2.1降级到django 2.0只是解决了我的问题!即使我在上面花了很多时间也很开心。请注意,它可以与Python 3.7配合使用(就像我最初的愿望一样。)

感谢阅读!