How to run Django in a command line terminal

Let’s say, for example, that you want to manually add certain objects without using the admin, or need to quickly check a function without creating unit tests for it. In that case, you will need to run Django-specific commands from a python shell.

If you import django or use shell, you will probably hit the following error:

Error: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.

The easiest way to circumvent this is to use the following command to open a Django shell:

python shell

This will automatically handle the settings module for you, and start a fresh shell from which you can create models, run functions and quickly test views.

Leave a Reply

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    Markdown is turned off in code blocks:
     [This is not a link](

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see