Bryan Wolfford
Bryan Wolfford

Reputation: 2192

Django documentation: Models; error from line 1 of code

In the Django documentation on Models, the first command that I am asked to run and python's response was:

>>> from django.db import models

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    from django.db import models
  File "C:\Python26\lib\site-packages\django\db\__init__.py", line 11, in <module>
        if DEFAULT_DB_ALIAS not in settings.DATABASES:
  File "C:\Python26\lib\site-packages\django\utils\functional.py", line 184, in inner
    self._setup()
  File "C:\Python26\lib\site-packages\django\conf\__init__.py", line 40, in _setup
    raise ImportError("Settings cannot be imported, because environment variable %s is             undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable     DJANGO_SETTINGS_MODULE is undefined.

As you can see from the stack trace, I am running a django server in python 2.6.6. Could anyone offer me a clue to start off on the right foot with this tutorial? Thanks in advance.

Upvotes: 0

Views: 623

Answers (3)

Bryan Wolfford
Bryan Wolfford

Reputation: 2192

The answer here definitely works for the interactive prompt, which I was using, however I don't think that the intention of the first block of code was intended to actually be run! Immediately following that first code in the models documentation, you are expected to put the next codes into your models.py file created during the previous tutorial... I guess that's why they subtly labeled that section "Quick Example." What a headache!

Futhermore, that paragraph goes on to say "The above Person model would create a database table like..." suggesting that it was never intended to actually be run as-is.

Upvotes: 0

Andrew Gorcester
Andrew Gorcester

Reputation: 19983

Are you running these commands from ./manage.py shell? You cannot run django commands from the regular python shell without specifying where your settings.py file is for the project. Django's ./manage.py shell command specifies that for you, making it easier to do django stuff on the command line.

If you don't see a file named manage.py in your current working directory, then that probably means you're not in the directory of your django application, or you haven't started one yet.

Edit: Also, that documentation is meant as an example of what to put in your application's models.py file, not something you should type in on the command line. That doesn't mean it won't work on the command line (if you use manage.py shell) but it isn't what the documentation is suggesting. Check the tutorial if you're unclear on how to start the shell and what files go where.

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798804

You're doing it wrong.

$ python manage.py shell

Upvotes: 2

Related Questions