Environment:
Ubuntu
Django 1.5
In virtualenv Environment
(WEB01) [email protected]:~/.virtualenvs/web01/dj_01/pro01$ pythonpython 2.7.6 (Default, Mar 22 2014, 22:59:56) [GCC 4.8.2] on linux2Type "Help", "copyright" , "credits" or "license" for more information.>>> from django import template>>> t = template. Template (' my name is {{ name }}. ') traceback (most recent call last): file "<stdin>", line 1, in <module> File "/home/milo/.virtualenvs/web01/local/lib/python2.7/ site-packages/django/template/base.py ", line 123, in __init__ if settings. template_debug and origin is none: file "/home/milo/.virtualenvs/web01/ local/lib/python2.7/site-packages/django/conf/__init__.py ", line 52, in __getaTtr__ self._setup (name) File "/home/milo/.virtualenvs/web01/local/lib/ python2.7/site-packages/django/conf/__init__.py ", line 45, in _setup % (desc, environment_variable)) django.core.exceptions.improperlyconfigured: requested setting TEMPLATE_DEBUG, but settings are not configured. You must either define the environment variable django_settings_module or Call settings.configure () before accessing settings.
Because of the lack of environment variables, we import environment variables. The workaround is as follows.
Method 1:
(WEB01) [Email protected]:~/.virtualenvs/web01/dj_01/pro01$ pythonpython 2.7.6 (default, Mar, 22:59:56) [GCC 4.8.2] on Lin Ux2type "Help", "copyright", "credits" or "license" for more information.>>> from django.conf import settings> >> settings.configure () >>> from django import template>>> t = template. Template (' My name is {{name}}}. ') >>> can be used normally, no error message.
Method 2:
(WEB01) [Email protected]:~/.virtualenvs/web01/dj_01/pro01$ python manage.py Shell # in this way into the interactive environment Python 2.7.6 (default, Mar 22 201 4, 22:59:56) [GCC 4.8.2] on Linux2type ' help ', ' copyright ', ' credits ' or ' license ' for more information. (Interactiveconsole) >>> from django import template>>> t = template. Template (' My name is {{name}}}. ') >>> can be used normally, no error message. Testing Django's interactive environment at ordinary times suggests entering interactive mode in this way to avoid unnecessary hassles.
Method 3:
(WEB01) [Email protected]:~/.virtualenvs/web01/dj_01/pro01$ vim ~/.BASHRC Add the following: Export django_settings_module= pro01.settings# where Pro01 is the Django project name # is actually the configuration file that invokes the project settings.py then executes: (WEB01) [Email protected]:~/.virtualenvs/web01/ dj_01/pro01$ source ~/.BASHRC [email protected]:~/.virtualenvs/web01/dj_01/pro01$ workon web01 (WEB01) [email protected ]:~/.virtualenvs/web01/dj_01/pro01$ Pythonpython 2.7.6 (default, Mar, 22:59:56) [GCC 4.8.2] on Linux2type ' help ', "Copyright", "credits" or "license" for more information.>>> from django import template>>> t = Template . Template (' My name is {{name}}}. ') >>>
#################################################################
Excerpt from the Django Book 2.0
Let's take a look at why the introduction of setting will give an error.
There are two ways to start Python: Python manage.py shell and Python.
If you want to know, here will explain how it works. The Django Search django_settings_module environment variable, which is set in settings.py. For example, suppose MySite is in your Python search path, then django_settings_module should be set to: ' Mysite.settings '.
When you run the command: Python manage.py shell, it will automatically handle django_settings_module for you. In the current examples, we encourage you to use the "Python manage.py shell" approach, which frees you from the hassle of configuring environment variables that you are unfamiliar with.
As you become more familiar with DJANGO, you may prefer to discard the use of ' manage.py shell ' instead of manually adding Django_settings_module this environment variable in your configuration file. Bash_profile.
So, two other solutions are:
1. launch Python using the Python manage.py shell
2. Manually add the Django_settings_module this environment variable in your configuration file. Bash_profile.
This article is from the "Me & Done" blog, so be sure to keep this source http://medone.blog.51cto.com/9469723/1576601
Python interactive environment, Django template error: django.core.exceptions.ImproperlyConfigured