Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does manage.py work?

Tags:

I just installed django and I'm doing the tutorial on their website.

I'm following their instructions on command line and they're working, but I'm wondering why?

For instance, you can access the command 'manage.py startapp xyz' and it would create a package, but when I look into manage.py, it only contains the following code (added by django, I didn't touch manage.py)

#!/usr/bin/env python import os import sys  if __name__ == "__main__":     os.environ.setdefault("DJANGO_SETTINGS_MODULE", "DjangoProject12.settings")      from django.core.management import execute_from_command_line      execute_from_command_line(sys.argv) 

I don't see any method for startapp in manage.py or anywhere else. I'm not sure if this is django specific or if there is some fundamental gap in my python knowledge. Thank you.

like image 259
user856358 Avatar asked Apr 19 '13 14:04

user856358


People also ask

What is manage py do?

manage.py : A command-line utility that lets you interact with this Django project in various ways. You can read all the details about manage.py in django-admin and manage.py. The inner mysite/ directory is the actual Python package for your project.

What is the difference between Django admin and manage py?

Django-admin.py: It is a Django's command line utility for administrative tasks. Manage.py: It is an automatically created file in each Django project.

Where should manage py be?

You can run manage.py from anywhere. No need to activate a virtualenv or be in the right directory. Simply run /path/to/virtualenv/bin/manage.py ... and it just works. If your virtualenv is activated, the path will already be set and manage.py will just work without the path.


1 Answers

You need to look in the django.core.management package; the execute_from_command_line() function accepts the sys.argv command line parameters and takes it from there.

When you enter manage.py startapp xyz on the command line, sys.argv is set to ['manage.py', 'startapp', 'xyz'].

These are handed off to a ManagementUtility instance, which has a .execute() method to do the actual parsing.

The whole django.core.management package is modular; the .commands sub package contains the various standard commands for the manage.py tool. django.core.management.commands.startapp handles the startapp subcommand, for example.

like image 189
Martijn Pieters Avatar answered Oct 28 '22 03:10

Martijn Pieters