module 'jinja2' has no attribute 'Environment'
老师您好!
我在看到4-2节时,完全按照老师的步骤做的。已经通过pip安装了jinja2.
我已经在templates目录下新建了detail.html
urls.py已经配置了
views.py已经配置了
settings.py已经配置了
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, { 'BACKEND': 'django.template.backends.jinja2.Jinja2', 'DIRS': [os.path.join(BASE_DIR,'jinja2')], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ]
我还没有将detail.html移动到jinja2目录下呢。
但我在访问http://127.0.0.1:8000/templ/show/是报错了
具体报错如下
Internal Server Error: /templ/show/ Traceback (most recent call last): File "C:\Users\Mr.Zhou\Envs\P3Django1.11\lib\site-packages\django\template\utils.py", line 65, in __getitem__ return self._engines[alias] KeyError: 'jinja2' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\Mr.Zhou\Envs\P3Django1.11\lib\site-packages\django\utils\module_loading.py", line 23, in import_string return getattr(module, class_name) AttributeError: module 'jinja2' has no attribute 'Environment' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\Mr.Zhou\Envs\P3Django1.11\lib\site-packages\django\core\handlers\exception.py", line 41, in inner response = get_response(request) File "C:\Users\Mr.Zhou\Envs\P3Django1.11\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response response = self.process_exception_by_middleware(e, request) File "C:\Users\Mr.Zhou\Envs\P3Django1.11\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "E:\LearnDjango\my_project\my_project\views.py", line 148, in templ_show return render(request,'detail.html') File "C:\Users\Mr.Zhou\Envs\P3Django1.11\lib\site-packages\django\shortcuts.py", line 30, in render content = loader.render_to_string(template_name, context, request, using=using) File "C:\Users\Mr.Zhou\Envs\P3Django1.11\lib\site-packages\django\template\loader.py", line 67, in render_to_string template = get_template(template_name, using=using) File "C:\Users\Mr.Zhou\Envs\P3Django1.11\lib\site-packages\django\template\loader.py", line 18, in get_template engines = _engine_list(using) File "C:\Users\Mr.Zhou\Envs\P3Django1.11\lib\site-packages\django\template\loader.py", line 72, in _engine_list return engines.all() if using is None else [engines[using]] File "C:\Users\Mr.Zhou\Envs\P3Django1.11\lib\site-packages\django\template\utils.py", line 89, in all return [self[alias] for alias in self] File "C:\Users\Mr.Zhou\Envs\P3Django1.11\lib\site-packages\django\template\utils.py", line 89, in <listcomp> return [self[alias] for alias in self] File "C:\Users\Mr.Zhou\Envs\P3Django1.11\lib\site-packages\django\template\utils.py", line 80, in __getitem__ engine = engine_cls(params) File "C:\Users\Mr.Zhou\Envs\P3Django1.11\lib\site-packages\django\template\backends\jinja2.py", line 29, in __init__ environment_cls = import_string(environment) File "C:\Users\Mr.Zhou\Envs\P3Django1.11\lib\site-packages\django\utils\module_loading.py", line 27, in import_string six.reraise(ImportError, ImportError(msg), sys.exc_info()[2]) File "C:\Users\Mr.Zhou\Envs\P3Django1.11\lib\site-packages\django\utils\six.py", line 685, in reraise raise value.with_traceback(tb) File "C:\Users\Mr.Zhou\Envs\P3Django1.11\lib\site-packages\django\utils\module_loading.py", line 23, in import_string return getattr(module, class_name) ImportError: Module "jinja2" does not define a "Environment" attribute/class [28/Aug/2019 23:56:25] "GET /templ/show/ HTTP/1.1" 500 122573
但是我将settings.py中关于jinja2的配置去掉,则访问不会报错。请问老师这是什么原因?该如何解决?
正在回答 回答被采纳积分+1
我按照克莱斯顿老师的步骤做了一遍,并没有解决这个问题,现在报错如下:
from jinja2 import Environment
ImportError: cannot import name 'Environment' from 'jinja2' (unknown location)
Internal Server Error: /templ/show/ Traceback (most recent call last): File "C:\Users\Mr.Zhou\Envs\P3Django1.11\lib\site-packages\django\template\utils.py", line 65, in __getitem__ return self._engines[alias] KeyError: 'jinja2' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\Mr.Zhou\Envs\P3Django1.11\lib\site-packages\django\core\handlers\exception.py", line 41, in inner response = get_response(request) File "C:\Users\Mr.Zhou\Envs\P3Django1.11\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response response = self.process_exception_by_middleware(e, request) File "C:\Users\Mr.Zhou\Envs\P3Django1.11\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "E:\LearnDjango\my_project\my_project\views.py", line 148, in templ_show return render(request,'detail.html') File "C:\Users\Mr.Zhou\Envs\P3Django1.11\lib\site-packages\django\shortcuts.py", line 30, in render content = loader.render_to_string(template_name, context, request, using=using) File "C:\Users\Mr.Zhou\Envs\P3Django1.11\lib\site-packages\django\template\loader.py", line 67, in render_to_string template = get_template(template_name, using=using) File "C:\Users\Mr.Zhou\Envs\P3Django1.11\lib\site-packages\django\template\loader.py", line 18, in get_template engines = _engine_list(using) File "C:\Users\Mr.Zhou\Envs\P3Django1.11\lib\site-packages\django\template\loader.py", line 72, in _engine_list return engines.all() if using is None else [engines[using]] File "C:\Users\Mr.Zhou\Envs\P3Django1.11\lib\site-packages\django\template\utils.py", line 89, in all return [self[alias] for alias in self] File "C:\Users\Mr.Zhou\Envs\P3Django1.11\lib\site-packages\django\template\utils.py", line 89, in <listcomp> return [self[alias] for alias in self] File "C:\Users\Mr.Zhou\Envs\P3Django1.11\lib\site-packages\django\template\utils.py", line 80, in __getitem__ engine = engine_cls(params) File "C:\Users\Mr.Zhou\Envs\P3Django1.11\lib\site-packages\django\template\backends\jinja2.py", line 29, in __init__ environment_cls = import_string(environment) File "C:\Users\Mr.Zhou\Envs\P3Django1.11\lib\site-packages\django\utils\module_loading.py", line 20, in import_string module = import_module(module_path) File "C:\Users\Mr.Zhou\Envs\P3Django1.11\lib\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 "E:\LearnDjango\my_project\my_project\jinja2.py", line 6, in <module> from jinja2 import Environment ImportError: cannot import name 'Environment' from 'jinja2' (unknown location) [29/Aug/2019 21:41:40] "GET /templ/show/ HTTP/1.1" 500 124316 Not Found: /templ/show/是报错了
我用的Jinja2版本是2.10.1,老师用的是2.10,就这一点不一样,就有这么大的差别吗?
同学你好:
这是由于jinja2版本的问题造成的。所以为了避免这样的问题。同学可以按照以下方式配置。
在test_django目录下创建一个jinja2文件。
from __future__ import absolute_import from django.contrib.staticfiles.storage import staticfiles_storage from django.urls import reverse from jinja2 import Environment def environment(**options): env = Environment(**options) env.globals.update({ 'static': staticfiles_storage.url, 'url': reverse, }) return env
在·setting.py文件中更改以下配置:
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, { 'BACKEND': 'django.template.backends.jinja2.Jinja2', 'DIRS': [os.path.join(BASE_DIR, 'templates')], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], 'environment': 'test_django.jinja2.environment', }, }, ]
如果我解决了同学的问题,请采纳!学习愉快^_^。
- 参与学习 人
- 提交作业 218 份
- 解答问题 3562 个
本阶段带你用Python开发一个网站,学习主流框架Django+Flask是Python Web开发的第一步,在基础知识上实现积分商城的项目开发,体验真实的项目开发流程,提高解决编程问题和效率的能力。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星