DJANGO_SETTINGS_MODULE is undefined
Asked Answered
A

4

5

when i input from django.db import models into IDLE,the result throw a exception named Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined. why does this exception occur and how i can fix it

Abattoir answered 20/9, 2011 at 2:41 Comment(0)
C
8

You can use python manage.py shell instead of using IDLE.

manage.py reads and uses django settings including DJANGO_SETTINGS_MODULE.

Celestial answered 14/2, 2013 at 10:26 Comment(0)
M
6

Ensure, if not, follow the way to Quick Start:

Assumptions:

  1. You have installed python on c:\ i.e., c:\python27
  2. You have installed Django 1.3 in same folder i.e., c:\python27

Step 1:

First you need to set system path for django-admin.py with:

 set DJANGO_PATH="c:\python27\scripts"

so that you will able to run django-admin.py from anywhere where you want to create DJango project.

Step 2:

Now go to c:\python27\ and run following syntax to create test project:

   django-admin.py startproject mysite  

Here "mysite" is our project name, you can choose whatever you want.

Now you project is created you can check this in c:\python27\ in folder named mysite\

Step 3:

Now go to python shell at c:\python27

set environment variable "DJANGO_SETTINGS_MODULE" with:

   >>> import os
   >>> os.environ['DJANGO_SETTINGS_MODULE']='mysite.settings'

Here " mysite " is project name which we created in step 2.

All basic setups are done now you can run your command

 from django.db import models<br>

If problem still persist please review all steps or reply me.

Manor answered 6/3, 2012 at 5:44 Comment(0)
R
5

Don't run django-admin.py for anything other than the initial project creation. For everything after that, use manage.py, which takes care of the finding the settings.

Racon answered 26/2, 2013 at 11:16 Comment(0)
A
3

It occurred because you forgot to supply the Django project settings. You can fix it by supplying them.

Acuna answered 20/9, 2011 at 2:53 Comment(3)
and how i can supply it in windowsAbattoir
Exactly the same way. Unless you're using the first method in cmd.exe, in which case you'd use set.Acuna
my question is how i can fix it,eg,may i add one line code in one py fileAbattoir

© 2022 - 2024 — McMap. All rights reserved.