Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Projects and Apps  ›  Lesson

manage.py — Your Project's Command Line

manage.py 10 minmanage.py is a small script that points Django at your project's settings and then runs management commands such as runserver, migrate and test.

manage.py — Your Project's Command Line

Almost everything you do to a Django project, other than editing code, starts with the same two words: python manage.py. Start the server, change the database, run the tests, open a shell. It is worth knowing what this one small file does before trusting it with all of that.

The rule

manage.py = Django's command runner, already pointed at your settings.

Open it and you will find a few lines. The important one is this:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cookbook.settings")

That tells Django which settings module to use; then the script passes your command on to Django. django-admin is the same runner without that line, which is why you used it only once, for startproject, before any settings existed.

The commands you will reach for most:

python manage.py runserver          # start the development server
python manage.py startapp reviews   # create a new app
python manage.py makemigrations     # write migration files from model changes
python manage.py migrate            # apply migrations to the database
python manage.py createsuperuser    # make an account for the admin site
python manage.py shell              # Python prompt with the project loaded
python manage.py test               # run the tests
python manage.py check              # look for configuration problems

When you forget one, ask the tool itself:

python manage.py help               # list every command
python manage.py help migrate       # options for one command

Apps can add commands too; installed packages often do, and they appear in help.

The shortcut

Read every command as python manage.py <verb> [target]. The verb says what to do; the target, if any, narrows it (migrate recipes, test recipes). Run it from the folder that contains manage.py, with the environment active.

A morning on the cookbookStep 1 of 4
  1. Start the server

    python manage.py runserver so you can see pages as you change them.

If a command answers "No module named django", the environment is not active. If it answers "can't open file 'manage.py'", you are in the wrong folder.

Drills

Answer fast — trust the rule.

quick drill

What does manage.py set before handing your command to Django?

quick drill

Which command opens a Python prompt with the cookbook project loaded?

quick drill

How do you see the options for the migrate command?

quick drill

Running python manage.py runserver prints "can't open file 'manage.py'". What is wrong?

Remember

  • manage.py is Django's command runner, already pointed at your settings.
  • Read commands as python manage.py <verb> [target]; help lists the verbs.
  • Trap: wrong folder or an inactive environment, not a broken project.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhy not django-admin?

Explain what manage.py does before running any command, why you use it instead of django-admin once the project exists, and name four commands you will use often with what each does.