Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fix my Django ImproperlyConfigured exception?

I've followed installations of Django using pip in a virtualenv since I'm learning Django. When the Django Book told me to type this in the Python shell,

from django import template
t = template.Template("My name is {{ name }}.")

I got this exception. I don't know how to solve this.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "django/template/base.py", line 123, in __init__
    if settings.TEMPLATE_DEBUG and origin is None:
  File "django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "django/conf/__init__.py", line 46, 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.

Can somebody walk me through? I'm using Mac OS X 10.8

like image 603
Nogurenn Avatar asked Oct 02 '13 03:10

Nogurenn


People also ask

How do I handle exceptions in Django?

Django Exception Example It raises a DoesNotExist exception if data not found. This is Django's built-in exception. It shows the following exception because no record is available at id 12. We can handle it by using try and except, now let's handle this exception.

What is Django exception explain with an example?

This type of exception is raised by django. urls when a requested view does not exist. This type of exception is raised when a middleware is not used in a server configuration. This type of exception is raised when Django is improperly configured, for example - when a value in settings.py is incorrect.

What is import error in Django?

db' pylint(import-error) showing up. This is because VS Code is not running the Virtual Environment of the app. To fix it, run cmd-shift-p (or click View -> Command Palette and run the command Python: Select Interpreter. VS Code will show you a list of Python interpreters found.

What is Django error?

Server errorsWhen DEBUG is False , Django will email the users listed in the ADMINS setting whenever your code raises an unhandled exception and results in an internal server error (strictly speaking, for any response with an HTTP status code of 500 or greater).


2 Answers

It seems that you are running python shell using the command python. So the DJANGO_SETTINGS_MODULE variable is not available. Run your application specific python shell using

python manage.py shell

Read more about manage.py.

manage.py sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project’s settings.py file.

like image 87
arulmr Avatar answered Sep 21 '22 22:09

arulmr


you need to run the django shell so it loads the settings

python manage.py shell
like image 38
toad013 Avatar answered Sep 21 '22 22:09

toad013