django.core.exceptions.ImproperlyConfigured 关于设置 TEMPLATES
Posted
技术标签:
【中文标题】django.core.exceptions.ImproperlyConfigured 关于设置 TEMPLATES【英文标题】:django.core.exceptions.ImproperlyConfigured about setting TEMPLATES 【发布时间】:2015-07-22 18:36:00 【问题描述】:关于 django.template 的问题
代码如下:
from django import template
t = template.Template('My name is name .')
但是当我跑的时候:
Traceback (most recent call last):
File "F:/daima/QPython/123.py", line 2, in <module>
t = template.Template('my name is name .')
File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\template\base.py", line 187, in __init__
engine = Engine.get_default()
File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\utils\lru_cache.py", line 125, in wrapper
result = user_function(*args, **kwds)
File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\template\engine.py", line 73, in get_default
django_engines = [engine for engine in engines.all()
File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\template\utils.py", line 108, in all
return [self[alias] for alias in self]
File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\template\utils.py", line 105, in __iter__
return iter(self.templates)
File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\utils\functional.py", line 60, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\template\utils.py", line 31, in templates
self._templates = settings.TEMPLATES
File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\conf\__init__.py", line 48, in __getattr__
self._setup(name)
File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\conf\__init__.py", line 42, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting TEMPLATES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
设置?出了什么问题?感谢您的帮助
【问题讨论】:
也许您缺少TEMPLATE_DIRS
设置?
但它只是一个python程序,不在django框架中。那是什么?
这是一个独立的程序吗?不是一个完整的 Django 项目?我不认为 django 是为此而生的。
只是一个带有django.template的python程序,不在django框架中。
How do I use Django templates without the rest of Django? 的可能重复项
【参考方案1】:
先导入设置。
from django.conf import settings
settings.configure()
from django import template
t = template.Template('My name is name .')
【讨论】:
以上是关于django.core.exceptions.ImproperlyConfigured 关于设置 TEMPLATES的主要内容,如果未能解决你的问题,请参考以下文章