1 =============================
2 django-admin.py and manage.py
3 =============================
5 ``django-admin.py`` is Django's command-line utility for administrative tasks.
6 This document outlines all it can do.
8 In addition, ``manage.py`` is automatically created in each Django project.
9 ``manage.py`` is a thin wrapper around ``django-admin.py`` that takes care of
10 two things for you before delegating to ``django-admin.py``:
12 * It puts your project's package on ``sys.path``.
14 * It sets the ``DJANGO_SETTINGS_MODULE`` environment variable so that it
15 points to your project's ``settings.py`` file.
17 The ``django-admin.py`` script should be on your system path if you installed
18 Django via its ``setup.py`` utility. If it's not on your path, you can find it in
19 ``site-packages/django/bin`` within your Python installation. Consider
20 symlinking it from some place on your path, such as ``/usr/local/bin``.
22 For Windows users, who do not have symlinking functionality available, you
23 can copy ``django-admin.py`` to a location on your existing path or edit the
24 ``PATH`` settings (under ``Settings - Control Panel - System - Advanced - Environment...``)
25 to point to its installed location.
27 Generally, when working on a single Django project, it's easier to use
28 ``manage.py``. Use ``django-admin.py`` with ``DJANGO_SETTINGS_MODULE``, or the
29 ``--settings`` command line option, if you need to switch between multiple
30 Django settings files.
32 The command-line examples throughout this document use ``django-admin.py`` to
33 be consistent, but any example can use ``manage.py`` just as well.
38 ``django-admin.py <subcommand> [options]``
40 ``manage.py <subcommand> [options]``
42 ``subcommand`` should be one of the subcommands listed in this document.
43 ``options``, which is optional, should be zero or more of the options available
44 for the given subcommand.
49 In Django 0.96, run ``django-admin.py --help`` to display a help message that
50 includes a terse list of all available subcommands and options.
52 In the Django development version, run ``django-admin.py help`` to display a
53 list of all available subcommands. Run ``django-admin.py help <subcommand>``
54 to display a description of the given subcommand and a list of its available
60 Many subcommands take a list of "app names." An "app name" is the basename of
61 the package containing your models. For example, if your ``INSTALLED_APPS``
62 contains the string ``'mysite.blog'``, the app name is ``blog``.
64 Determining the version
65 -----------------------
67 Run ``django-admin.py --version`` to display the current Django version.
78 adminindex <appname appname ...>
79 --------------------------------
81 Prints the admin-index template snippet for the given app name(s).
83 Use admin-index template snippets if you want to customize the look and feel of
84 your admin's index page. See `Tutorial 2`_ for more information.
86 .. _Tutorial 2: ../tutorial02/
88 createcachetable <tablename>
89 ----------------------------
91 Creates a cache table named ``tablename`` for use with the database cache
92 backend. See the `cache documentation`_ for more information.
94 .. _cache documentation: ../cache/
99 **New in Django development version**
101 Creates a superuser account (a user who has all permissions). This is
102 useful if you need to create an initial superuser account but did not
103 do so during ``syncdb``, or if you need to programmatically generate
104 superuser accounts for your site(s).
106 When run interactively, this command will prompt for a password for
107 the new superuser account. When run non-interactively, no password
108 will be set, and the superuser account will not be able to log in until
109 a password has been manually set for it.
111 The username and e-mail address for the new account can be supplied by
112 using the ``--username`` and ``--email`` arguments on the command
113 line. If either of those is not supplied, ``createsuperuser`` will prompt for
114 it when running interactively.
116 This command is only available if Django's `authentication system`_
117 (``django.contrib.auth``) is installed.
119 .. _authentication system: ../authentication/
124 Runs the command-line client for the database engine specified in your
125 ``DATABASE_ENGINE`` setting, with the connection parameters specified in your
126 ``DATABASE_USER``, ``DATABASE_PASSWORD``, etc., settings.
128 * For PostgreSQL, this runs the ``psql`` command-line client.
129 * For MySQL, this runs the ``mysql`` command-line client.
130 * For SQLite, this runs the ``sqlite3`` command-line client.
132 This command assumes the programs are on your ``PATH`` so that a simple call to
133 the program name (``psql``, ``mysql``, ``sqlite3``) will find the program in
134 the right place. There's no way to specify the location of the program
140 Displays differences between the current settings file and Django's default
143 Settings that don't appear in the defaults are followed by ``"###"``. For
144 example, the default settings don't define ``ROOT_URLCONF``, so
145 ``ROOT_URLCONF`` is followed by ``"###"`` in the output of ``diffsettings``.
147 Note that Django's default settings live in ``django/conf/global_settings.py``,
148 if you're ever curious to see the full list of defaults.
150 dumpdata <appname appname ...>
151 ------------------------------
153 Outputs to standard output all data in the database associated with the named
156 If no application name is provided, all installed applications will be dumped.
158 The output of ``dumpdata`` can be used as input for ``loaddata``.
160 Note that ``dumpdata`` uses the default manager on the model for selecting the
161 records to dump. If you're using a `custom manager`_ as the default manager
162 and it filters some of the available records, not all of the objects will be
165 .. _custom manager: ../model-api/#custom-managers
170 **New in Django development version**
172 Exclude a specific application from the applications whose contents is
173 output. For example, to specifically exclude the `auth` application from
174 the output, you would call::
176 django-admin.py dumpdata --exclude=auth
178 If you want to exclude multiple applications, use multiple ``--exclude``
181 django-admin.py dumpdata --exclude=auth --exclude=contenttype
186 By default, ``dumpdata`` will format its output in JSON, but you can use the
187 ``--format`` option to specify another format. Currently supported formats are
188 listed in `Serialization formats`_.
192 django-admin.py dumpdata --format=xml
194 .. _Serialization formats: ../serialization/#serialization-formats
199 By default, ``dumpdata`` will output all data on a single line. This isn't easy
200 for humans to read, so you can use the ``--indent`` option to pretty-print the
201 output with a number of indentation spaces.
205 django-admin.py dumpdata --indent=4
210 Returns the database to the state it was in immediately after syncdb was
211 executed. This means that all data will be removed from the database, any
212 post-synchronization handlers will be re-executed, and the ``initial_data``
213 fixture will be re-installed.
215 The behavior of this command has changed in the Django development version.
216 Previously, this command cleared *every* table in the database, including any
217 table that Django didn't know about (i.e., tables that didn't have associated
218 models and/or weren't in ``INSTALLED_APPS``). Now, the command only clears
219 tables that are represented by Django models and are activated in
225 Use the ``--noinput`` option to suppress all user prompting, such as
226 "Are you sure?" confirmation messages. This is useful if ``django-admin.py``
227 is being executed as an unattended, automated script.
232 Use ``--verbosity`` to specify the amount of notification and debug information
233 that ``django-admin.py`` should print to the console.
235 * ``0`` means no output.
236 * ``1`` means normal output (default).
237 * ``2`` means verbose output.
241 django-admin.py flush --verbosity=2
246 Introspects the database tables in the database pointed-to by the
247 ``DATABASE_NAME`` setting and outputs a Django model module (a ``models.py``
248 file) to standard output.
250 Use this if you have a legacy database with which you'd like to use Django.
251 The script will inspect the database and create a model for each table within
254 As you might expect, the created models will have an attribute for every field
255 in the table. Note that ``inspectdb`` has a few special cases in its field-name
258 * If ``inspectdb`` cannot map a column's type to a model field type, it'll
259 use ``TextField`` and will insert the Python comment
260 ``'This field type is a guess.'`` next to the field in the generated
263 * If the database column name is a Python reserved word (such as
264 ``'pass'``, ``'class'`` or ``'for'``), ``inspectdb`` will append
265 ``'_field'`` to the attribute name. For example, if a table has a column
266 ``'for'``, the generated model will have a field ``'for_field'``, with
267 the ``db_column`` attribute set to ``'for'``. ``inspectdb`` will insert
269 ``'Field renamed because it was a Python reserved word.'`` next to the
272 This feature is meant as a shortcut, not as definitive model generation. After
273 you run it, you'll want to look over the generated models yourself to make
274 customizations. In particular, you'll need to rearrange models' order, so that
275 models that refer to other models are ordered properly.
277 Primary keys are automatically introspected for PostgreSQL, MySQL and
278 SQLite, in which case Django puts in the ``primary_key=True`` where
281 ``inspectdb`` works with PostgreSQL, MySQL and SQLite. Foreign-key detection
282 only works in PostgreSQL and with certain types of MySQL tables.
284 loaddata <fixture fixture ...>
285 ------------------------------
287 Searches for and loads the contents of the named fixture into the database.
289 A *fixture* is a collection of files that contain the serialized contents of
290 the database. Each fixture has a unique name, and the files that comprise the
291 fixture can be distributed over multiple directories, in multiple applications.
293 Django will search in three locations for fixtures:
295 1. In the ``fixtures`` directory of every installed application
296 2. In any directory named in the ``FIXTURE_DIRS`` setting
297 3. In the literal path named by the fixture
299 Django will load any and all fixtures it finds in these locations that match
300 the provided fixture names.
302 If the named fixture has a file extension, only fixtures of that type
303 will be loaded. For example::
305 django-admin.py loaddata mydata.json
307 would only load JSON fixtures called ``mydata``. The fixture extension
308 must correspond to the registered name of a serializer (e.g., ``json`` or
311 If you omit the extension, Django will search all available fixture types
312 for a matching fixture. For example::
314 django-admin.py loaddata mydata
316 would look for any fixture of any fixture type called ``mydata``. If a fixture
317 directory contained ``mydata.json``, that fixture would be loaded
318 as a JSON fixture. However, if two fixtures with the same name but different
319 fixture type are discovered (for example, if ``mydata.json`` and
320 ``mydata.xml`` were found in the same fixture directory), fixture
321 installation will be aborted, and any data installed in the call to
322 ``loaddata`` will be removed from the database.
324 The fixtures that are named can include directory components. These
325 directories will be included in the search path. For example::
327 django-admin.py loaddata foo/bar/mydata.json
329 would search ``<appname>/fixtures/foo/bar/mydata.json`` for each installed
330 application, ``<dirname>/foo/bar/mydata.json`` for each directory in
331 ``FIXTURE_DIRS``, and the literal path ``foo/bar/mydata.json``.
333 Note that the order in which fixture files are processed is undefined. However,
334 all fixture data is installed as a single transaction, so data in
335 one fixture can reference data in another fixture. If the database backend
336 supports row-level constraints, these constraints will be checked at the
337 end of the transaction.
339 The ``dumpdata`` command can be used to generate input for ``loaddata``.
341 .. admonition:: MySQL and Fixtures
343 Unfortunately, MySQL isn't capable of completely supporting all the
344 features of Django fixtures. If you use MyISAM tables, MySQL doesn't
345 support transactions or constraints, so you won't get a rollback if
346 multiple transaction files are found, or validation of fixture data.
347 If you use InnoDB tables, you won't be able to have any forward
348 references in your data files - MySQL doesn't provide a mechanism to
349 defer checking of row constraints until a transaction is committed.
354 Use ``--verbosity`` to specify the amount of notification and debug information
355 that ``django-admin.py`` should print to the console.
357 * ``0`` means no output.
358 * ``1`` means normal output (default).
359 * ``2`` means verbose output.
363 django-admin.py loaddata --verbosity=2
365 reset <appname appname ...>
366 ---------------------------
368 Executes the equivalent of ``sqlreset`` for the given app name(s).
373 Use the ``--noinput`` option to suppress all user prompting, such as
374 "Are you sure?" confirmation messages. This is useful if ``django-admin.py``
375 is being executed as an unattended, automated script.
380 Starts a set of FastCGI processes suitable for use with any Web server
381 that supports the FastCGI protocol. See the `FastCGI deployment
382 documentation`_ for details. Requires the Python FastCGI module from
385 .. _FastCGI deployment documentation: ../fastcgi/
386 .. _flup: http://www.saddi.com/software/flup/
388 runserver [optional port number, or ipaddr:port]
389 ------------------------------------------------
391 Starts a lightweight development Web server on the local machine. By default,
392 the server runs on port 8000 on the IP address 127.0.0.1. You can pass in an
393 IP address and port number explicitly.
395 If you run this script as a user with normal privileges (recommended), you
396 might not have access to start a port on a low port number. Low port numbers
397 are reserved for the superuser (root).
399 DO NOT USE THIS SERVER IN A PRODUCTION SETTING. It has not gone through
400 security audits or performance tests. (And that's how it's gonna stay. We're in
401 the business of making Web frameworks, not Web servers, so improving this
402 server to be able to handle a production environment is outside the scope of
405 The development server automatically reloads Python code for each request, as
406 needed. You don't need to restart the server for code changes to take effect.
408 When you start the server, and each time you change Python code while the
409 server is running, the server will validate all of your installed models. (See
410 the ``validate`` command below.) If the validator finds errors, it will print
411 them to standard output, but it won't stop the server.
413 You can run as many servers as you want, as long as they're on separate ports.
414 Just execute ``django-admin.py runserver`` more than once.
416 Note that the default IP address, 127.0.0.1, is not accessible from other
417 machines on your network. To make your development server viewable to other
418 machines on the network, use its own IP address (e.g. ``192.168.2.1``) or
424 Use the ``--adminmedia`` option to tell Django where to find the various CSS
425 and JavaScript files for the Django admin interface. Normally, the development
426 server serves these files out of the Django source tree magically, but you'd
427 want to use this if you made any changes to those files for your own site.
431 django-admin.py runserver --adminmedia=/tmp/new-admin-style/
436 Use the ``--noreload`` option to disable the use of the auto-reloader. This
437 means any Python code changes you make while the server is running will *not*
438 take effect if the particular Python modules have already been loaded into
443 django-admin.py runserver --noreload
445 Examples of using different ports and addresses
446 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
448 Port 8000 on IP address 127.0.0.1::
450 django-admin.py runserver
452 Port 8000 on IP address 1.2.3.4::
454 django-admin.py runserver 1.2.3.4:8000
456 Port 7000 on IP address 127.0.0.1::
458 django-admin.py runserver 7000
460 Port 7000 on IP address 1.2.3.4::
462 django-admin.py runserver 1.2.3.4:7000
464 Serving static files with the development server
465 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
467 By default, the development server doesn't serve any static files for your site
468 (such as CSS files, images, things under ``MEDIA_URL`` and so forth). If
469 you want to configure Django to serve static media, read the `serving static files`_
472 .. _serving static files: ../static_files/
477 Starts the Python interactive interpreter.
479 Django will use IPython_, if it's installed. If you have IPython installed and
480 want to force use of the "plain" Python interpreter, use the ``--plain``
483 django-admin.py shell --plain
485 .. _IPython: http://ipython.scipy.org/
487 sql <appname appname ...>
488 -------------------------
490 Prints the CREATE TABLE SQL statements for the given app name(s).
492 sqlall <appname appname ...>
493 ----------------------------
495 Prints the CREATE TABLE and initial-data SQL statements for the given app name(s).
497 Refer to the description of ``sqlcustom`` for an explanation of how to
498 specify initial data.
500 sqlclear <appname appname ...>
501 ------------------------------
503 Prints the DROP TABLE SQL statements for the given app name(s).
505 sqlcustom <appname appname ...>
506 -------------------------------
508 Prints the custom SQL statements for the given app name(s).
510 For each model in each specified app, this command looks for the file
511 ``<appname>/sql/<modelname>.sql``, where ``<appname>`` is the given app name and
512 ``<modelname>`` is the model's name in lowercase. For example, if you have an
513 app ``news`` that includes a ``Story`` model, ``sqlcustom`` will attempt
514 to read a file ``news/sql/story.sql`` and append it to the output of this
517 Each of the SQL files, if given, is expected to contain valid SQL. The SQL
518 files are piped directly into the database after all of the models'
519 table-creation statements have been executed. Use this SQL hook to make any
520 table modifications, or insert any SQL functions into the database.
522 Note that the order in which the SQL files are processed is undefined.
527 Prints the SQL statements that would be executed for the `flush`_ command.
529 sqlindexes <appname appname ...>
530 --------------------------------
532 Prints the CREATE INDEX SQL statements for the given app name(s).
534 sqlreset <appname appname ...>
535 ------------------------------
537 Prints the DROP TABLE SQL, then the CREATE TABLE SQL, for the given app name(s).
539 sqlsequencereset <appname appname ...>
540 --------------------------------------
542 Prints the SQL statements for resetting sequences for the given app name(s).
544 See http://simon.incutio.com/archive/2004/04/21/postgres for more information.
549 Creates a Django app directory structure for the given app name in the current
552 startproject <projectname>
553 --------------------------
555 Creates a Django project directory structure for the given project name in the
561 Creates the database tables for all apps in ``INSTALLED_APPS`` whose tables
562 have not already been created.
564 Use this command when you've added new applications to your project and want to
565 install them in the database. This includes any apps shipped with Django that
566 might be in ``INSTALLED_APPS`` by default. When you start a new project, run
567 this command to install the default apps.
569 .. admonition:: Syncdb will not alter existing tables
571 ``syncdb`` will only create tables for models which have not yet been
572 installed. It will *never* issue ``ALTER TABLE`` statements to match
573 changes made to a model class after installation. Changes to model classes
574 and database schemas often involve some form of ambiguity and, in those
575 cases, Django would have to guess at the correct changes to make. There is
576 a risk that critical data would be lost in the process.
578 If you have made changes to a model and wish to alter the database tables
579 to match, use the ``sql`` command to display the new SQL structure and
580 compare that to your existing table schema to work out the changes.
582 If you're installing the ``django.contrib.auth`` application, ``syncdb`` will
583 give you the option of creating a superuser immediately.
585 ``syncdb`` will also search for and install any fixture named ``initial_data``
586 with an appropriate extension (e.g. ``json`` or ``xml``). See the
587 documentation for ``loaddata`` for details on the specification of fixture
593 Use ``--verbosity`` to specify the amount of notification and debug information
594 that ``django-admin.py`` should print to the console.
596 * ``0`` means no output.
597 * ``1`` means normal output (default).
598 * ``2`` means verbose output.
602 django-admin.py syncdb --verbosity=2
607 Use the ``--noinput`` option to suppress all user prompting, such as
608 "Are you sure?" confirmation messages. This is useful if ``django-admin.py``
609 is being executed as an unattended, automated script.
614 Runs tests for all installed models. See `Testing Django applications`_
615 for more information.
617 .. _testing Django applications: ../testing/
622 Use the ``--noinput`` option to suppress all user prompting, such as
623 "Are you sure?" confirmation messages. This is useful if ``django-admin.py``
624 is being executed as an unattended, automated script.
629 Use ``--verbosity`` to specify the amount of notification and debug information
630 that ``django-admin.py`` should print to the console.
632 * ``0`` means no output.
633 * ``1`` means normal output (default).
634 * ``2`` means verbose output.
638 django-admin.py test --verbosity=2
640 testserver <fixture fixture ...>
641 --------------------------------
643 **New in Django development version**
645 Runs a Django development server (as in ``runserver``) using data from the
648 For example, this command::
650 django-admin.py testserver mydata.json
652 ...would perform the following steps:
654 1. Create a test database, as described in `testing Django applications`_.
655 2. Populate the test database with fixture data from the given fixtures.
656 (For more on fixtures, see the documentation for ``loaddata`` above.)
657 3. Runs the Django development server (as in ``runserver``), pointed at
658 this newly created test database instead of your production database.
660 This is useful in a number of ways:
662 * When you're writing `unit tests`_ of how your views act with certain
663 fixture data, you can use ``testserver`` to interact with the views in
664 a Web browser, manually.
666 * Let's say you're developing your Django application and have a "pristine"
667 copy of a database that you'd like to interact with. You can dump your
668 database to a fixture (using the ``dumpdata`` command, explained above),
669 then use ``testserver`` to run your Web application with that data. With
670 this arrangement, you have the flexibility of messing up your data
671 in any way, knowing that whatever data changes you're making are only
672 being made to a test database.
674 Note that this server does *not* automatically detect changes to your Python
675 source code (as ``runserver`` does). It does, however, detect changes to
678 .. _unit tests: ../testing/
680 --addrport [port number or ipaddr:port]
681 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
683 Use ``--addrport`` to specify a different port, or IP address and port, from
684 the default of 127.0.0.1:8000. This value follows exactly the same format and
685 serves exactly the same function as the argument to the ``runserver`` subcommand.
689 To run the test server on port 7000 with ``fixture1`` and ``fixture2``::
691 django-admin.py testserver --addrport 7000 fixture1 fixture2
692 django-admin.py testserver fixture1 fixture2 --addrport 7000
694 (The above statements are equivalent. We include both of them to demonstrate
695 that it doesn't matter whether the options come before or after the fixture
698 To run on 1.2.3.4:7000 with a ``test`` fixture::
700 django-admin.py testserver --addrport 1.2.3.4:7000 test
705 Use ``--verbosity`` to specify the amount of notification and debug information
706 that ``django-admin.py`` should print to the console.
708 * ``0`` means no output.
709 * ``1`` means normal output (default).
710 * ``2`` means verbose output.
714 django-admin.py testserver --verbosity=2
719 Validates all installed models (according to the ``INSTALLED_APPS`` setting)
720 and prints validation errors to standard output.
725 Although some subcommands may allow their own custom options, every subcommand
726 allows for the following options:
733 django-admin.py syncdb --pythonpath='/home/djangoprojects/myproject'
735 Adds the given filesystem path to the Python `import search path`_. If this
736 isn't provided, ``django-admin.py`` will use the ``PYTHONPATH`` environment
739 Note that this option is unnecessary in ``manage.py``, because it takes care of
740 setting the Python path for you.
742 .. _import search path: http://diveintopython.org/getting_to_know_python/everything_is_an_object.html
749 django-admin.py syncdb --settings=mysite.settings
751 Explicitly specifies the settings module to use. The settings module should be
752 in Python package syntax, e.g. ``mysite.settings``. If this isn't provided,
753 ``django-admin.py`` will use the ``DJANGO_SETTINGS_MODULE`` environment
756 Note that this option is unnecessary in ``manage.py``, because it uses
757 ``settings.py`` from the current project by default.
764 django-admin.py syncdb --traceback
766 By default, ``django-admin.py`` will show a simple error message whenever an
767 error occurs. If you specify ``--traceback``, ``django-admin.py`` will
768 output a full stack trace whenever an exception is raised.
776 The ``django-admin.py`` / ``manage.py`` commands that output SQL to standard
777 output will use pretty color-coded output if your terminal supports
778 ANSI-colored output. It won't use the color codes if you're piping the
779 command's output to another program.
784 If you use the Bash shell, consider installing the Django bash completion
785 script, which lives in ``extras/django_bash_completion`` in the Django
786 distribution. It enables tab-completion of ``django-admin.py`` and
787 ``manage.py`` commands, so you can, for instance...
789 * Type ``django-admin.py``.
790 * Press [TAB] to see all available options.
791 * Type ``sql``, then [TAB], to see all available options whose names start
797 **New in Django development version**
799 Applications can register their own actions with ``manage.py``. For example,
800 you might want to add a ``manage.py`` action for a Django app that you're
803 To do this, just add a ``management/commands`` directory to your application.
804 Each Python module in that directory will be auto-discovered and registered as
805 a command that can be executed as an action when you run ``manage.py``::
817 In this example, the ``explode`` command will be made available to any project
818 that includes the ``blog`` application in ``settings.INSTALLED_APPS``.
820 The ``explode.py`` module has only one requirement -- it must define a class
821 called ``Command`` that extends ``django.core.management.base.BaseCommand``.
823 For more details on how to define your own commands, look at the code for the
824 existing ``django-admin.py`` commands, in ``/django/core/management/commands``.