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 problemsWhen you forget one, ask the tool itself:
python manage.py help # list every command
python manage.py help migrate # options for one commandApps 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.
Start the server
python manage.py runserverso 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.
What does manage.py set before handing your command to Django?
Which command opens a Python prompt with the cookbook project loaded?
How do you see the options for the migrate command?
Running python manage.py runserver prints "can't open file 'manage.py'". What is wrong?
Remember
manage.pyis Django's command runner, already pointed at your settings.- Read commands as
python manage.py <verb> [target];helplists the verbs. - Trap: wrong folder or an inactive environment, not a broken project.
