[Django] After the template is introduced, the error requested setting template_debug, but settings are not configured.

Source: Internet
Author: User

I am trying to write the following code today:

>>> from django import template >>> t = template.Template('My name is {{ name }}.') >>> c = template.Context({'name': 'WHY'}) >>> print t.render(c) My name is WHY. >>> c = template.Context({'name': 'HuaHua'}) >>> print t.render(c) My name is HuaHua.

A large number of errors were reported:

  1. Traceback (most recent call last): file "<stdin>", line 1, in <module>
  2. File "/usr/local/lib/python2.7/dist-packages/Django-1.5.dev20120922131713-py2.7.egg/Django/template/base. py ",
  3. Line 123, in _ init _ If settings. template_debug and origin is none: file "/usr/local/lib/python2.7/dist-packages/Django-1.5.dev20120922131713-py2.7.egg/Django/CONF/_ init __. PY ", line 50, in _ getattr _ self. _ setup (name) file "/usr/local/lib/python2.7/dist-packages/Django-1.5.dev20120922131713-py2.7.egg/Django/CONF/_ init __. PY ", line 43, in _ setup % (name, environment_variable) Django. core. exceptions. improperlyconfigured: requested


After a rough look, it is said that the configuration is not configured and environment variables need to be configured.

You can use the following methods to solve the problem:

Before import, add the following code to import settings:

>>> from django.conf import settings    >>> settings.configure()  

The problem is solved.

Of course, there are two other solutions. Before that, let's take a look at why setting is not introduced.

There are two ways to start Python: Python manage. py shell and python.

Both commands start the interactive interpreter, but the manage. py shell command has an important difference: Before starting the interpreter, it tells Django which setting file to use. Most subsystems of the Django framework, including the template system, depend on the configuration file. If Django does not know which configuration file to use, these systems will not work.

If you want to know, I will explain how it works. Django searches for the change in the django_settings_module environment, which is set in settings. py. For example, if mysite is in your python search path, django_settings_module should be set to 'mysite. setting '.
When you run the command: Python manage. py shell, it will automatically Help You With django_settings_module. In the current examples, we encourage you to use ''python manage. PY shell '', which saves you the trouble of configuring environment variables you are not familiar.
As you get familiar with Django, you may prefer to discard using ''manage. py shell'', but manually add the environment variable django_settings_module in your configuration file. bash_profile.

Therefore, the other two solutions are:

1. Start Python using Python manage. py Shell

2. manually add the environment variable django_settings_module in your configuration file. bash_profile.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.