django-admin.txt 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788
  1. ==========================
  2. django-admin and manage.py
  3. ==========================
  4. ``django-admin`` is Django's command-line utility for administrative tasks.
  5. This document outlines all it can do.
  6. In addition, ``manage.py`` is automatically created in each Django project.
  7. ``manage.py`` does the same thing as ``django-admin`` but takes care of a few
  8. things for you:
  9. * It puts your project's package on ``sys.path``.
  10. * It sets the :envvar:`DJANGO_SETTINGS_MODULE` environment variable so that
  11. it points to your project's ``settings.py`` file.
  12. The ``django-admin`` script should be on your system path if you installed
  13. Django via its ``setup.py`` utility. If it's not on your path, you can find it
  14. in ``site-packages/django/bin`` within your Python installation. Consider
  15. symlinking it from some place on your path, such as ``/usr/local/bin``.
  16. For Windows users, who do not have symlinking functionality available, you can
  17. copy ``django-admin.exe`` to a location on your existing path or edit the
  18. ``PATH`` settings (under ``Settings - Control Panel - System - Advanced -
  19. Environment...``) to point to its installed location.
  20. Generally, when working on a single Django project, it's easier to use
  21. ``manage.py`` than ``django-admin``. If you need to switch between multiple
  22. Django settings files, use ``django-admin`` with
  23. :envvar:`DJANGO_SETTINGS_MODULE` or the :djadminopt:`--settings` command line
  24. option.
  25. The command-line examples throughout this document use ``django-admin`` to
  26. be consistent, but any example can use ``manage.py`` or ``python -m django``
  27. just as well.
  28. .. versionadded:: 1.9
  29. ``python -m django`` was added.
  30. Usage
  31. =====
  32. .. code-block:: console
  33. $ django-admin <command> [options]
  34. $ manage.py <command> [options]
  35. $ python -m django <command> [options]
  36. ``command`` should be one of the commands listed in this document.
  37. ``options``, which is optional, should be zero or more of the options available
  38. for the given command.
  39. Getting runtime help
  40. --------------------
  41. .. django-admin:: help
  42. Run ``django-admin help`` to display usage information and a list of the
  43. commands provided by each application.
  44. Run ``django-admin help --commands`` to display a list of all available
  45. commands.
  46. Run ``django-admin help <command>`` to display a description of the given
  47. command and a list of its available options.
  48. App names
  49. ---------
  50. Many commands take a list of "app names." An "app name" is the basename of
  51. the package containing your models. For example, if your :setting:`INSTALLED_APPS`
  52. contains the string ``'mysite.blog'``, the app name is ``blog``.
  53. Determining the version
  54. -----------------------
  55. .. django-admin:: version
  56. Run ``django-admin version`` to display the current Django version.
  57. The output follows the schema described in :pep:`386`::
  58. 1.4.dev17026
  59. 1.4a1
  60. 1.4
  61. Displaying debug output
  62. -----------------------
  63. Use :djadminopt:`--verbosity` to specify the amount of notification and debug information
  64. that ``django-admin`` should print to the console. For more details, see the
  65. documentation for the :djadminopt:`--verbosity` option.
  66. Available commands
  67. ==================
  68. check <appname appname ...>
  69. ---------------------------
  70. .. django-admin:: check
  71. Uses the :doc:`system check framework </ref/checks>` to inspect
  72. the entire Django project for common problems.
  73. The system check framework will confirm that there aren't any problems with
  74. your installed models or your admin registrations. It will also provide warnings
  75. of common compatibility problems introduced by upgrading Django to a new version.
  76. Custom checks may be introduced by other libraries and applications.
  77. By default, all apps will be checked. You can check a subset of apps by providing
  78. a list of app labels as arguments::
  79. python manage.py check auth admin myapp
  80. If you do not specify any app, all apps will be checked.
  81. .. django-admin-option:: --tag <tagname>
  82. The :doc:`system check framework </ref/checks>` performs many different
  83. types of checks. These check types are categorized with tags. You can use these tags
  84. to restrict the checks performed to just those in a particular category. For example,
  85. to perform only security and compatibility checks, you would run::
  86. python manage.py check --tag security --tag compatibility
  87. .. django-admin-option:: --list-tags
  88. List all available tags.
  89. .. django-admin-option:: --deploy
  90. The ``--deploy`` option activates some additional checks that are only relevant
  91. in a deployment setting.
  92. You can use this option in your local development environment, but since your
  93. local development settings module may not have many of your production settings,
  94. you will probably want to point the ``check`` command at a different settings
  95. module, either by setting the ``DJANGO_SETTINGS_MODULE`` environment variable,
  96. or by passing the ``--settings`` option::
  97. python manage.py check --deploy --settings=production_settings
  98. Or you could run it directly on a production or staging deployment to verify
  99. that the correct settings are in use (omitting ``--settings``). You could even
  100. make it part of your integration test suite.
  101. .. django-admin-option:: --fail-level
  102. .. versionadded:: 1.10
  103. Specifies the message level that will cause the command to exit with a non-zero
  104. status. Default is ``ERROR``.
  105. Available levels are: ``CRITICAL``, ``ERROR``, ``WARNING``, ``INFO``, and
  106. ``DEBUG``.
  107. compilemessages
  108. ---------------
  109. .. django-admin:: compilemessages
  110. Compiles .po files created by :djadmin:`makemessages` to .mo files for use with
  111. the builtin gettext support. See :doc:`/topics/i18n/index`.
  112. Use the :djadminopt:`--locale` option (or its shorter version ``-l``) to
  113. specify the locale(s) to process. If not provided, all locales are processed.
  114. Use the :djadminopt:`--exclude` option (or its shorter version ``-x``) to
  115. specify the locale(s) to exclude from processing. If not provided, no locales
  116. are excluded.
  117. You can pass ``--use-fuzzy`` option (or ``-f``) to include fuzzy translations
  118. into compiled files.
  119. .. versionchanged:: 1.9
  120. ``compilemessages`` now matches the operation of :djadmin:`makemessages`,
  121. scanning the project tree for ``.po`` files to compile.
  122. Example usage::
  123. django-admin compilemessages --locale=pt_BR
  124. django-admin compilemessages --locale=pt_BR --locale=fr -f
  125. django-admin compilemessages -l pt_BR
  126. django-admin compilemessages -l pt_BR -l fr --use-fuzzy
  127. django-admin compilemessages --exclude=pt_BR
  128. django-admin compilemessages --exclude=pt_BR --exclude=fr
  129. django-admin compilemessages -x pt_BR
  130. django-admin compilemessages -x pt_BR -x fr
  131. createcachetable
  132. ----------------
  133. .. django-admin:: createcachetable
  134. Creates the cache tables for use with the database cache backend using the
  135. information from your settings file. See :doc:`/topics/cache` for more
  136. information.
  137. The :djadminopt:`--database` option can be used to specify the database
  138. onto which the cache table will be installed, but since this information is
  139. pulled from your settings by default, it's typically not needed.
  140. The :djadminopt:`--dry-run` option will print the SQL that would be run without
  141. actually running it, so you can customize it or use the migrations framework.
  142. .. versionchanged:: 1.9
  143. The ``--dry-run`` option was added.
  144. dbshell
  145. -------
  146. .. django-admin:: dbshell
  147. Runs the command-line client for the database engine specified in your
  148. ``ENGINE`` setting, with the connection parameters specified in your
  149. :setting:`USER`, :setting:`PASSWORD`, etc., settings.
  150. * For PostgreSQL, this runs the ``psql`` command-line client.
  151. * For MySQL, this runs the ``mysql`` command-line client.
  152. * For SQLite, this runs the ``sqlite3`` command-line client.
  153. * For Oracle, this runs the ``sqlplus`` command-line client.
  154. This command assumes the programs are on your ``PATH`` so that a simple call to
  155. the program name (``psql``, ``mysql``, ``sqlite3``, ``sqlplus``) will find the
  156. program in the right place. There's no way to specify the location of the
  157. program manually.
  158. The :djadminopt:`--database` option can be used to specify the database
  159. onto which to open a shell.
  160. diffsettings
  161. ------------
  162. .. django-admin:: diffsettings
  163. Displays differences between the current settings file and Django's default
  164. settings.
  165. Settings that don't appear in the defaults are followed by ``"###"``. For
  166. example, the default settings don't define :setting:`ROOT_URLCONF`, so
  167. :setting:`ROOT_URLCONF` is followed by ``"###"`` in the output of
  168. ``diffsettings``.
  169. The :djadminopt:`--all` option may be provided to display all settings, even
  170. if they have Django's default value. Such settings are prefixed by ``"###"``.
  171. dumpdata <app_label app_label app_label.Model ...>
  172. --------------------------------------------------
  173. .. django-admin:: dumpdata
  174. Outputs to standard output all data in the database associated with the named
  175. application(s).
  176. If no application name is provided, all installed applications will be dumped.
  177. The output of ``dumpdata`` can be used as input for :djadmin:`loaddata`.
  178. Note that ``dumpdata`` uses the default manager on the model for selecting the
  179. records to dump. If you're using a :ref:`custom manager <custom-managers>` as
  180. the default manager and it filters some of the available records, not all of the
  181. objects will be dumped.
  182. The :djadminopt:`--all` option may be provided to specify that
  183. ``dumpdata`` should use Django's base manager, dumping records which
  184. might otherwise be filtered or modified by a custom manager.
  185. .. django-admin-option:: --format <fmt>
  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
  188. are listed in :ref:`serialization-formats`.
  189. .. django-admin-option:: --indent <num>
  190. By default, ``dumpdata`` will output all data on a single line. This isn't
  191. easy for humans to read, so you can use the ``--indent`` option to
  192. pretty-print the output with a number of indentation spaces.
  193. The :djadminopt:`--exclude` option may be provided to prevent specific
  194. applications or models (specified as in the form of ``app_label.ModelName``)
  195. from being dumped. If you specify a model name to ``dumpdata``, the dumped
  196. output will be restricted to that model, rather than the entire application.
  197. You can also mix application names and model names.
  198. The :djadminopt:`--database` option can be used to specify the database
  199. from which data will be dumped.
  200. .. django-admin-option:: --natural-foreign
  201. When this option is specified, Django will use the ``natural_key()`` model
  202. method to serialize any foreign key and many-to-many relationship to objects of
  203. the type that defines the method. If you are dumping ``contrib.auth``
  204. ``Permission`` objects or ``contrib.contenttypes`` ``ContentType`` objects, you
  205. should probably be using this flag. See the :ref:`natural keys
  206. <topics-serialization-natural-keys>` documentation for more details on this
  207. and the next option.
  208. .. django-admin-option:: --natural-primary
  209. When this option is specified, Django will not provide the primary key in the
  210. serialized data of this object since it can be calculated during
  211. deserialization.
  212. .. django-admin-option:: --pks
  213. By default, ``dumpdata`` will output all the records of the model, but
  214. you can use the ``--pks`` option to specify a comma separated list of
  215. primary keys on which to filter. This is only available when dumping
  216. one model.
  217. .. django-admin-option:: --output
  218. By default ``dumpdata`` will output all the serialized data to standard output.
  219. This option allows you to specify the file to which the data is to be written.
  220. When this option is set and the verbosity is greater than 0 (the default), a
  221. progress bar is shown in the terminal.
  222. .. versionchanged:: 1.9
  223. The progress bar in the terminal was added.
  224. flush
  225. -----
  226. .. django-admin:: flush
  227. Removes all data from the database and re-executes any post-synchronization
  228. handlers. The table of which migrations have been applied is not cleared.
  229. If you would rather start from an empty database and re-run all migrations, you
  230. should drop and recreate the database and then run :djadmin:`migrate` instead.
  231. The :djadminopt:`--noinput` option may be provided to suppress all user
  232. prompts.
  233. The :djadminopt:`--database` option may be used to specify the database
  234. to flush.
  235. inspectdb
  236. ---------
  237. .. django-admin:: inspectdb
  238. Introspects the database tables in the database pointed-to by the
  239. :setting:`NAME` setting and outputs a Django model module (a ``models.py``
  240. file) to standard output.
  241. Use this if you have a legacy database with which you'd like to use Django.
  242. The script will inspect the database and create a model for each table within
  243. it.
  244. As you might expect, the created models will have an attribute for every field
  245. in the table. Note that ``inspectdb`` has a few special cases in its field-name
  246. output:
  247. * If ``inspectdb`` cannot map a column's type to a model field type, it'll
  248. use ``TextField`` and will insert the Python comment
  249. ``'This field type is a guess.'`` next to the field in the generated
  250. model.
  251. * If the database column name is a Python reserved word (such as
  252. ``'pass'``, ``'class'`` or ``'for'``), ``inspectdb`` will append
  253. ``'_field'`` to the attribute name. For example, if a table has a column
  254. ``'for'``, the generated model will have a field ``'for_field'``, with
  255. the ``db_column`` attribute set to ``'for'``. ``inspectdb`` will insert
  256. the Python comment
  257. ``'Field renamed because it was a Python reserved word.'`` next to the
  258. field.
  259. This feature is meant as a shortcut, not as definitive model generation. After
  260. you run it, you'll want to look over the generated models yourself to make
  261. customizations. In particular, you'll need to rearrange models' order, so that
  262. models that refer to other models are ordered properly.
  263. Primary keys are automatically introspected for PostgreSQL, MySQL and
  264. SQLite, in which case Django puts in the ``primary_key=True`` where
  265. needed.
  266. ``inspectdb`` works with PostgreSQL, MySQL and SQLite. Foreign-key detection
  267. only works in PostgreSQL and with certain types of MySQL tables.
  268. Django doesn't create database defaults when a
  269. :attr:`~django.db.models.Field.default` is specified on a model field.
  270. Similarly, database defaults aren't translated to model field defaults or
  271. detected in any fashion by ``inspectdb``.
  272. By default, ``inspectdb`` creates unmanaged models. That is, ``managed = False``
  273. in the model's ``Meta`` class tells Django not to manage each table's creation,
  274. modification, and deletion. If you do want to allow Django to manage the
  275. table's lifecycle, you'll need to change the
  276. :attr:`~django.db.models.Options.managed` option to ``True`` (or simply remove
  277. it because ``True`` is its default value).
  278. The :djadminopt:`--database` option may be used to specify the
  279. database to introspect.
  280. loaddata <fixture fixture ...>
  281. ------------------------------
  282. .. django-admin:: loaddata
  283. Searches for and loads the contents of the named fixture into the database.
  284. The :djadminopt:`--database` option can be used to specify the database
  285. onto which the data will be loaded.
  286. .. django-admin-option:: --ignorenonexistent
  287. The :djadminopt:`--ignorenonexistent` option can be used to ignore fields and
  288. models that may have been removed since the fixture was originally generated.
  289. .. django-admin-option:: --app
  290. The :djadminopt:`--app` option can be used to specify a single app to look
  291. for fixtures in rather than looking through all apps.
  292. What's a "fixture"?
  293. ~~~~~~~~~~~~~~~~~~~
  294. A *fixture* is a collection of files that contain the serialized contents of
  295. the database. Each fixture has a unique name, and the files that comprise the
  296. fixture can be distributed over multiple directories, in multiple applications.
  297. Django will search in three locations for fixtures:
  298. 1. In the ``fixtures`` directory of every installed application
  299. 2. In any directory named in the :setting:`FIXTURE_DIRS` setting
  300. 3. In the literal path named by the fixture
  301. Django will load any and all fixtures it finds in these locations that match
  302. the provided fixture names.
  303. If the named fixture has a file extension, only fixtures of that type
  304. will be loaded. For example::
  305. django-admin loaddata mydata.json
  306. would only load JSON fixtures called ``mydata``. The fixture extension
  307. must correspond to the registered name of a
  308. :ref:`serializer <serialization-formats>` (e.g., ``json`` or ``xml``).
  309. If you omit the extensions, Django will search all available fixture types
  310. for a matching fixture. For example::
  311. django-admin loaddata mydata
  312. would look for any fixture of any fixture type called ``mydata``. If a fixture
  313. directory contained ``mydata.json``, that fixture would be loaded
  314. as a JSON fixture.
  315. The fixtures that are named can include directory components. These
  316. directories will be included in the search path. For example::
  317. django-admin loaddata foo/bar/mydata.json
  318. would search ``<app_label>/fixtures/foo/bar/mydata.json`` for each installed
  319. application, ``<dirname>/foo/bar/mydata.json`` for each directory in
  320. :setting:`FIXTURE_DIRS`, and the literal path ``foo/bar/mydata.json``.
  321. When fixture files are processed, the data is saved to the database as is.
  322. Model defined :meth:`~django.db.models.Model.save` methods are not called, and
  323. any :data:`~django.db.models.signals.pre_save` or
  324. :data:`~django.db.models.signals.post_save` signals will be called with
  325. ``raw=True`` since the instance only contains attributes that are local to the
  326. model. You may, for example, want to disable handlers that access
  327. related fields that aren't present during fixture loading and would otherwise
  328. raise an exception::
  329. from django.db.models.signals import post_save
  330. from .models import MyModel
  331. def my_handler(**kwargs):
  332. # disable the handler during fixture loading
  333. if kwargs['raw']:
  334. return
  335. ...
  336. post_save.connect(my_handler, sender=MyModel)
  337. You could also write a simple decorator to encapsulate this logic::
  338. from functools import wraps
  339. def disable_for_loaddata(signal_handler):
  340. """
  341. Decorator that turns off signal handlers when loading fixture data.
  342. """
  343. @wraps(signal_handler)
  344. def wrapper(*args, **kwargs):
  345. if kwargs['raw']:
  346. return
  347. signal_handler(*args, **kwargs)
  348. return wrapper
  349. @disable_for_loaddata
  350. def my_handler(**kwargs):
  351. ...
  352. Just be aware that this logic will disable the signals whenever fixtures are
  353. deserialized, not just during ``loaddata``.
  354. Note that the order in which fixture files are processed is undefined. However,
  355. all fixture data is installed as a single transaction, so data in
  356. one fixture can reference data in another fixture. If the database backend
  357. supports row-level constraints, these constraints will be checked at the
  358. end of the transaction.
  359. The :djadmin:`dumpdata` command can be used to generate input for ``loaddata``.
  360. Compressed fixtures
  361. ~~~~~~~~~~~~~~~~~~~
  362. Fixtures may be compressed in ``zip``, ``gz``, or ``bz2`` format. For example::
  363. django-admin loaddata mydata.json
  364. would look for any of ``mydata.json``, ``mydata.json.zip``,
  365. ``mydata.json.gz``, or ``mydata.json.bz2``. The first file contained within a
  366. zip-compressed archive is used.
  367. Note that if two fixtures with the same name but different
  368. fixture type are discovered (for example, if ``mydata.json`` and
  369. ``mydata.xml.gz`` were found in the same fixture directory), fixture
  370. installation will be aborted, and any data installed in the call to
  371. ``loaddata`` will be removed from the database.
  372. .. admonition:: MySQL with MyISAM and fixtures
  373. The MyISAM storage engine of MySQL doesn't support transactions or
  374. constraints, so if you use MyISAM, you won't get validation of fixture
  375. data, or a rollback if multiple transaction files are found.
  376. Database-specific fixtures
  377. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  378. If you're in a multi-database setup, you might have fixture data that
  379. you want to load onto one database, but not onto another. In this
  380. situation, you can add a database identifier into the names of your fixtures.
  381. For example, if your :setting:`DATABASES` setting has a 'master' database
  382. defined, name the fixture ``mydata.master.json`` or
  383. ``mydata.master.json.gz`` and the fixture will only be loaded when you
  384. specify you want to load data into the ``master`` database.
  385. makemessages
  386. ------------
  387. .. django-admin:: makemessages
  388. Runs over the entire source tree of the current directory and pulls out all
  389. strings marked for translation. It creates (or updates) a message file in the
  390. conf/locale (in the Django tree) or locale (for project and application)
  391. directory. After making changes to the messages files you need to compile them
  392. with :djadmin:`compilemessages` for use with the builtin gettext support. See
  393. the :ref:`i18n documentation <how-to-create-language-files>` for details.
  394. .. django-admin-option:: --all
  395. Use the ``--all`` or ``-a`` option to update the message files for all
  396. available languages.
  397. Example usage::
  398. django-admin makemessages --all
  399. .. django-admin-option:: --extension
  400. Use the ``--extension`` or ``-e`` option to specify a list of file extensions
  401. to examine (default: ".html", ".txt").
  402. Example usage::
  403. django-admin makemessages --locale=de --extension xhtml
  404. Separate multiple extensions with commas or use -e or --extension multiple times::
  405. django-admin makemessages --locale=de --extension=html,txt --extension xml
  406. Use the :djadminopt:`--locale` option (or its shorter version ``-l``) to
  407. specify the locale(s) to process.
  408. Use the :djadminopt:`--exclude` option (or its shorter version ``-x``) to
  409. specify the locale(s) to exclude from processing. If not provided, no locales
  410. are excluded.
  411. Example usage::
  412. django-admin makemessages --locale=pt_BR
  413. django-admin makemessages --locale=pt_BR --locale=fr
  414. django-admin makemessages -l pt_BR
  415. django-admin makemessages -l pt_BR -l fr
  416. django-admin makemessages --exclude=pt_BR
  417. django-admin makemessages --exclude=pt_BR --exclude=fr
  418. django-admin makemessages -x pt_BR
  419. django-admin makemessages -x pt_BR -x fr
  420. .. django-admin-option:: --domain
  421. Use the ``--domain`` or ``-d`` option to change the domain of the messages files.
  422. Currently supported:
  423. * ``django`` for all ``*.py``, ``*.html`` and ``*.txt`` files (default)
  424. * ``djangojs`` for ``*.js`` files
  425. .. django-admin-option:: --symlinks
  426. Use the ``--symlinks`` or ``-s`` option to follow symlinks to directories when
  427. looking for new translation strings.
  428. Example usage::
  429. django-admin makemessages --locale=de --symlinks
  430. .. django-admin-option:: --ignore
  431. Use the ``--ignore`` or ``-i`` option to ignore files or directories matching
  432. the given :mod:`glob`-style pattern. Use multiple times to ignore more.
  433. These patterns are used by default: ``'CVS'``, ``'.*'``, ``'*~'``, ``'*.pyc'``
  434. Example usage::
  435. django-admin makemessages --locale=en_US --ignore=apps/* --ignore=secret/*.html
  436. .. django-admin-option:: --no-default-ignore
  437. Use the ``--no-default-ignore`` option to disable the default values of
  438. :djadminopt:`--ignore`.
  439. .. django-admin-option:: --no-wrap
  440. Use the ``--no-wrap`` option to disable breaking long message lines into
  441. several lines in language files.
  442. .. django-admin-option:: --no-location
  443. Use the ``--no-location`` option to suppress writing '``#: filename:line``’
  444. comment lines in language files. Note that using this option makes it harder
  445. for technically skilled translators to understand each message's context.
  446. .. django-admin-option:: --keep-pot
  447. Use the ``--keep-pot`` option to prevent Django from deleting the temporary
  448. ``.pot`` files it generates before creating the .po file. This is useful for
  449. debugging errors which may prevent the final language files from being created.
  450. .. seealso::
  451. See :ref:`customizing-makemessages` for instructions on how to customize
  452. the keywords that :djadmin:`makemessages` passes to ``xgettext``.
  453. makemigrations [<app_label>]
  454. ----------------------------
  455. .. django-admin:: makemigrations
  456. Creates new migrations based on the changes detected to your models.
  457. Migrations, their relationship with apps and more are covered in depth in
  458. :doc:`the migrations documentation</topics/migrations>`.
  459. Providing one or more app names as arguments will limit the migrations created
  460. to the app(s) specified and any dependencies needed (the table at the other end
  461. of a ``ForeignKey``, for example).
  462. .. versionchanged:: 1.9
  463. The ``--noinput`` option may be provided to suppress all user prompts. If a suppressed
  464. prompt cannot be resolved automatically, the command will exit with error code 3.
  465. .. django-admin-option:: --empty
  466. The ``--empty`` option will cause ``makemigrations`` to output an empty
  467. migration for the specified apps, for manual editing. This option is only
  468. for advanced users and should not be used unless you are familiar with
  469. the migration format, migration operations, and the dependencies between
  470. your migrations.
  471. .. django-admin-option:: --dry-run
  472. The ``--dry-run`` option shows what migrations would be made without
  473. actually writing any migrations files to disk. Using this option along with
  474. ``--verbosity 3`` will also show the complete migrations files that would be
  475. written.
  476. .. django-admin-option:: --merge
  477. The ``--merge`` option enables fixing of migration conflicts.
  478. .. django-admin-option:: --name, -n
  479. The ``--name`` option allows you to give the migration(s) a custom name instead
  480. of a generated one.
  481. .. django-admin-option:: --exit, -e
  482. .. deprecated:: 1.10
  483. Use the :djadminopt:`--check` option instead.
  484. The ``--exit`` option will cause ``makemigrations`` to exit with error code 1
  485. when no migrations are created (or would have been created, if combined with
  486. ``--dry-run``).
  487. .. django-admin-option:: --check
  488. .. versionadded:: 1.10
  489. The ``--check`` option makes ``makemigrations`` exit with a non-zero status
  490. when model changes without migrations are detected.
  491. migrate [<app_label> [<migrationname>]]
  492. ---------------------------------------
  493. .. django-admin:: migrate
  494. Synchronizes the database state with the current set of models and migrations.
  495. Migrations, their relationship with apps and more are covered in depth in
  496. :doc:`the migrations documentation</topics/migrations>`.
  497. The behavior of this command changes depending on the arguments provided:
  498. * No arguments: All apps have all of their migrations run.
  499. * ``<app_label>``: The specified app has its migrations run, up to the most
  500. recent migration. This may involve running other apps' migrations too, due
  501. to dependencies.
  502. * ``<app_label> <migrationname>``: Brings the database schema to a state where
  503. the named migration is applied, but no later migrations in the same app are
  504. applied. This may involve unapplying migrations if you have previously
  505. migrated past the named migration. Use the name ``zero`` to unapply all
  506. migrations for an app.
  507. The :djadminopt:`--database` option can be used to specify the database to
  508. migrate.
  509. .. django-admin-option:: --fake
  510. The ``--fake`` option tells Django to mark the migrations as having been
  511. applied or unapplied, but without actually running the SQL to change your
  512. database schema.
  513. This is intended for advanced users to manipulate the
  514. current migration state directly if they're manually applying changes;
  515. be warned that using ``--fake`` runs the risk of putting the migration state
  516. table into a state where manual recovery will be needed to make migrations
  517. run correctly.
  518. .. django-admin-option:: --fake-initial
  519. The ``--fake-initial`` option can be used to allow Django to skip an app's
  520. initial migration if all database tables with the names of all models created
  521. by all :class:`~django.db.migrations.operations.CreateModel` operations in that
  522. migration already exist. This option is intended for use when first running
  523. migrations against a database that preexisted the use of migrations. This
  524. option does not, however, check for matching database schema beyond matching
  525. table names and so is only safe to use if you are confident that your existing
  526. schema matches what is recorded in your initial migration.
  527. .. django-admin-option:: --run-syncdb
  528. .. versionadded:: 1.9
  529. The ``--run-syncdb`` option allows creating tables for apps without migrations.
  530. While this isn't recommended, the migrations framework is sometimes too slow
  531. on large projects with hundreds of models.
  532. runserver [port or address:port]
  533. --------------------------------
  534. .. django-admin:: runserver
  535. Starts a lightweight development Web server on the local machine. By default,
  536. the server runs on port 8000 on the IP address ``127.0.0.1``. You can pass in an
  537. IP address and port number explicitly.
  538. If you run this script as a user with normal privileges (recommended), you
  539. might not have access to start a port on a low port number. Low port numbers
  540. are reserved for the superuser (root).
  541. This server uses the WSGI application object specified by the
  542. :setting:`WSGI_APPLICATION` setting.
  543. DO NOT USE THIS SERVER IN A PRODUCTION SETTING. It has not gone through
  544. security audits or performance tests. (And that's how it's gonna stay. We're in
  545. the business of making Web frameworks, not Web servers, so improving this
  546. server to be able to handle a production environment is outside the scope of
  547. Django.)
  548. The development server automatically reloads Python code for each request, as
  549. needed. You don't need to restart the server for code changes to take effect.
  550. However, some actions like adding files don't trigger a restart, so you'll
  551. have to restart the server in these cases.
  552. If you are using Linux and install `pyinotify`_, kernel signals will be used to
  553. autoreload the server (rather than polling file modification timestamps each
  554. second). This offers better scaling to large projects, reduction in response
  555. time to code modification, more robust change detection, and battery usage
  556. reduction.
  557. .. _pyinotify: https://pypi.python.org/pypi/pyinotify/
  558. When you start the server, and each time you change Python code while the
  559. server is running, the system check framework will check your entire Django
  560. project for some common errors (see the :djadmin:`check` command). If any
  561. errors are found, they will be printed to standard output.
  562. You can run as many concurrent servers as you want, as long as they're on
  563. separate ports. Just execute ``django-admin runserver`` more than once.
  564. Note that the default IP address, ``127.0.0.1``, is not accessible from other
  565. machines on your network. To make your development server viewable to other
  566. machines on the network, use its own IP address (e.g. ``192.168.2.1``) or
  567. ``0.0.0.0`` or ``::`` (with IPv6 enabled).
  568. You can provide an IPv6 address surrounded by brackets
  569. (e.g. ``[200a::1]:8000``). This will automatically enable IPv6 support.
  570. A hostname containing ASCII-only characters can also be used.
  571. If the :doc:`staticfiles</ref/contrib/staticfiles>` contrib app is enabled
  572. (default in new projects) the :djadmin:`runserver` command will be overridden
  573. with its own :ref:`runserver<staticfiles-runserver>` command.
  574. If :djadmin:`migrate` was not previously executed, the table that stores the
  575. history of migrations is created at first run of ``runserver``.
  576. .. django-admin-option:: --noreload
  577. Use the ``--noreload`` option to disable the use of the auto-reloader. This
  578. means any Python code changes you make while the server is running will *not*
  579. take effect if the particular Python modules have already been loaded into
  580. memory.
  581. Example usage::
  582. django-admin runserver --noreload
  583. .. django-admin-option:: --nothreading
  584. The development server is multithreaded by default. Use the ``--nothreading``
  585. option to disable the use of threading in the development server.
  586. .. django-admin-option:: --ipv6, -6
  587. Use the ``--ipv6`` (or shorter ``-6``) option to tell Django to use IPv6 for
  588. the development server. This changes the default IP address from
  589. ``127.0.0.1`` to ``::1``.
  590. Example usage::
  591. django-admin runserver --ipv6
  592. Examples of using different ports and addresses
  593. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  594. Port 8000 on IP address ``127.0.0.1``::
  595. django-admin runserver
  596. Port 8000 on IP address ``1.2.3.4``::
  597. django-admin runserver 1.2.3.4:8000
  598. Port 7000 on IP address ``127.0.0.1``::
  599. django-admin runserver 7000
  600. Port 7000 on IP address ``1.2.3.4``::
  601. django-admin runserver 1.2.3.4:7000
  602. Port 8000 on IPv6 address ``::1``::
  603. django-admin runserver -6
  604. Port 7000 on IPv6 address ``::1``::
  605. django-admin runserver -6 7000
  606. Port 7000 on IPv6 address ``2001:0db8:1234:5678::9``::
  607. django-admin runserver [2001:0db8:1234:5678::9]:7000
  608. Port 8000 on IPv4 address of host ``localhost``::
  609. django-admin runserver localhost:8000
  610. Port 8000 on IPv6 address of host ``localhost``::
  611. django-admin runserver -6 localhost:8000
  612. Serving static files with the development server
  613. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  614. By default, the development server doesn't serve any static files for your site
  615. (such as CSS files, images, things under :setting:`MEDIA_URL` and so forth). If
  616. you want to configure Django to serve static media, read
  617. :doc:`/howto/static-files/index`.
  618. sendtestemail
  619. -------------
  620. .. django-admin:: sendtestemail
  621. .. versionadded:: 1.9
  622. Sends a test email (to confirm email sending through Django is working) to the
  623. recipient(s) specified. For example::
  624. django-admin sendtestemail foo@example.com bar@example.com
  625. .. django-admin-option:: --managers
  626. Use the ``--managers`` option to mail the email addresses specified in
  627. :setting:`MANAGERS` using :meth:`~django.core.mail.mail_managers()`.
  628. .. django-admin-option:: --admins
  629. Use the ``--admins`` option to mail the email addresses specified in
  630. :setting:`ADMINS` using :meth:`~django.core.mail.mail_admins()`.
  631. Note that you may use any combination of these options together.
  632. shell
  633. -----
  634. .. django-admin:: shell
  635. Starts the Python interactive interpreter.
  636. Django will use IPython_ or bpython_ if either is installed. If you have a
  637. rich shell installed but want to force use of the "plain" Python interpreter,
  638. use the ``-i python`` or ``--interface python`` option, like so::
  639. django-admin shell -i python
  640. django-admin shell --interface python
  641. .. deprecated:: 1.10
  642. In older versions, use the ``--plain`` option. This is deprecated and will
  643. be removed in Django 2.0.
  644. If you would like to specify either IPython or bpython as your interpreter if
  645. you have both installed you can specify an alternative interpreter interface
  646. with the ``-i`` or ``--interface`` options like so:
  647. IPython::
  648. django-admin shell -i ipython
  649. django-admin shell --interface ipython
  650. bpython::
  651. django-admin shell -i bpython
  652. django-admin shell --interface bpython
  653. .. _IPython: http://ipython.scipy.org/
  654. .. _bpython: http://bpython-interpreter.org/
  655. When the "plain" Python interactive interpreter starts (be it because
  656. ``--interface python`` was specified or because no other interactive interface
  657. is available) it reads the script pointed to by the :envvar:`PYTHONSTARTUP`
  658. environment variable and the ``~/.pythonrc.py`` script. If you don't wish this
  659. behavior you can use the ``--no-startup`` option. e.g.::
  660. django-admin shell --interface python --no-startup
  661. showmigrations [<app_label> [<app_label>]]
  662. ------------------------------------------
  663. .. django-admin:: showmigrations
  664. Shows all migrations in a project.
  665. .. django-admin-option:: --list, -l
  666. The ``--list`` option lists all of the apps Django knows about, the
  667. migrations available for each app, and whether or not each migration is
  668. applied (marked by an ``[X]`` next to the migration name).
  669. Apps without migrations are also listed, but have ``(no migrations)`` printed
  670. under them.
  671. .. django-admin-option:: --plan, -p
  672. The ``--plan`` option shows the migration plan Django will follow to apply
  673. migrations. Any supplied app labels are ignored because the plan might go
  674. beyond those apps. Same as ``--list``, applied migrations are marked by an
  675. ``[X]``. For a verbosity of 2 and above, all dependencies of a migration will
  676. also be shown.
  677. sqlflush
  678. --------
  679. .. django-admin:: sqlflush
  680. Prints the SQL statements that would be executed for the :djadmin:`flush`
  681. command.
  682. The :djadminopt:`--database` option can be used to specify the database for
  683. which to print the SQL.
  684. sqlmigrate <app_label> <migrationname>
  685. --------------------------------------
  686. .. django-admin:: sqlmigrate
  687. Prints the SQL for the named migration. This requires an active database
  688. connection, which it will use to resolve constraint names; this means you must
  689. generate the SQL against a copy of the database you wish to later apply it on.
  690. Note that ``sqlmigrate`` doesn't colorize its output.
  691. The :djadminopt:`--database` option can be used to specify the database for
  692. which to generate the SQL.
  693. .. django-admin-option:: --backwards
  694. By default, the SQL created is for running the migration in the forwards
  695. direction. Pass ``--backwards`` to generate the SQL for
  696. unapplying the migration instead.
  697. .. versionchanged:: 1.9
  698. To increase the readability of the overall SQL output the SQL code
  699. generated for each migration operation is preceded by the operation's
  700. description.
  701. sqlsequencereset <app_label app_label ...>
  702. ------------------------------------------
  703. .. django-admin:: sqlsequencereset
  704. Prints the SQL statements for resetting sequences for the given app name(s).
  705. Sequences are indexes used by some database engines to track the next available
  706. number for automatically incremented fields.
  707. Use this command to generate SQL which will fix cases where a sequence is out
  708. of sync with its automatically incremented field data.
  709. The :djadminopt:`--database` option can be used to specify the database for
  710. which to print the SQL.
  711. squashmigrations <app_label> [<start_migration_name>] <migration_name>
  712. ----------------------------------------------------------------------
  713. .. django-admin:: squashmigrations
  714. Squashes the migrations for ``app_label`` up to and including ``migration_name``
  715. down into fewer migrations, if possible. The resulting squashed migrations
  716. can live alongside the unsquashed ones safely. For more information,
  717. please read :ref:`migration-squashing`.
  718. .. versionadded:: 1.9
  719. When ``start_migration_name`` is given, Django will only include migrations
  720. starting from and including this migration. This helps to mitigate the
  721. squashing limitation of :class:`~django.db.migrations.operations.RunPython` and
  722. :class:`django.db.migrations.operations.RunSQL` migration operations.
  723. .. django-admin-option:: --no-optimize
  724. By default, Django will try to optimize the operations in your migrations
  725. to reduce the size of the resulting file. Pass ``--no-optimize`` if this
  726. process is failing for you or creating incorrect migrations, though please
  727. also file a Django bug report about the behavior, as optimization is meant
  728. to be safe.
  729. startapp <app_label> [destination]
  730. ----------------------------------
  731. .. django-admin:: startapp
  732. Creates a Django app directory structure for the given app name in the current
  733. directory or the given destination.
  734. By default the directory created contains a ``models.py`` file and other app
  735. template files. (See the `source`_ for more details.) If only the app
  736. name is given, the app directory will be created in the current working
  737. directory.
  738. If the optional destination is provided, Django will use that existing
  739. directory rather than creating a new one. You can use '.' to denote the current
  740. working directory.
  741. For example::
  742. django-admin startapp myapp /Users/jezdez/Code/myapp
  743. .. _custom-app-and-project-templates:
  744. .. django-admin-option:: --template
  745. With the ``--template`` option, you can use a custom app template by providing
  746. either the path to a directory with the app template file, or a path to a
  747. compressed file (``.tar.gz``, ``.tar.bz2``, ``.tgz``, ``.tbz``, ``.zip``)
  748. containing the app template files.
  749. For example, this would look for an app template in the given directory when
  750. creating the ``myapp`` app::
  751. django-admin startapp --template=/Users/jezdez/Code/my_app_template myapp
  752. Django will also accept URLs (``http``, ``https``, ``ftp``) to compressed
  753. archives with the app template files, downloading and extracting them on the
  754. fly.
  755. For example, taking advantage of GitHub's feature to expose repositories as
  756. zip files, you can use a URL like::
  757. django-admin startapp --template=https://github.com/githubuser/django-app-template/archive/master.zip myapp
  758. When Django copies the app template files, it also renders certain files
  759. through the template engine: the files whose extensions match the
  760. ``--extension`` option (``py`` by default) and the files whose names are passed
  761. with the ``--name`` option. The :class:`template context
  762. <django.template.Context>` used is:
  763. - Any option passed to the ``startapp`` command (among the command's supported
  764. options)
  765. - ``app_name`` -- the app name as passed to the command
  766. - ``app_directory`` -- the full path of the newly created app
  767. - ``camel_case_app_name`` -- the app name in camel case format
  768. - ``docs_version`` -- the version of the documentation: ``'dev'`` or ``'1.x'``
  769. .. versionadded:: 1.9
  770. ``camel_case_app_name`` was added.
  771. .. _render_warning:
  772. .. warning::
  773. When the app template files are rendered with the Django template
  774. engine (by default all ``*.py`` files), Django will also replace all
  775. stray template variables contained. For example, if one of the Python files
  776. contains a docstring explaining a particular feature related
  777. to template rendering, it might result in an incorrect example.
  778. To work around this problem, you can use the :ttag:`templatetag`
  779. templatetag to "escape" the various parts of the template syntax.
  780. .. _source: https://github.com/django/django/tree/master/django/conf/app_template/
  781. startproject <projectname> [destination]
  782. ----------------------------------------
  783. .. django-admin:: startproject
  784. Creates a Django project directory structure for the given project name in
  785. the current directory or the given destination.
  786. By default, the new directory contains ``manage.py`` and a project package
  787. (containing a ``settings.py`` and other files). See the `template source`_ for
  788. details.
  789. If only the project name is given, both the project directory and project
  790. package will be named ``<projectname>`` and the project directory
  791. will be created in the current working directory.
  792. If the optional destination is provided, Django will use that existing
  793. directory as the project directory, and create ``manage.py`` and the project
  794. package within it. Use '.' to denote the current working directory.
  795. For example::
  796. django-admin startproject myproject /Users/jezdez/Code/myproject_repo
  797. As with the :djadmin:`startapp` command, the ``--template`` option lets you
  798. specify a directory, file path or URL of a custom project template. See the
  799. :djadmin:`startapp` documentation for details of supported project template
  800. formats.
  801. For example, this would look for a project template in the given directory
  802. when creating the ``myproject`` project::
  803. django-admin startproject --template=/Users/jezdez/Code/my_project_template myproject
  804. Django will also accept URLs (``http``, ``https``, ``ftp``) to compressed
  805. archives with the project template files, downloading and extracting them on the
  806. fly.
  807. For example, taking advantage of GitHub's feature to expose repositories as
  808. zip files, you can use a URL like::
  809. django-admin startproject --template=https://github.com/githubuser/django-project-template/archive/master.zip myproject
  810. When Django copies the project template files, it also renders certain files
  811. through the template engine: the files whose extensions match the
  812. ``--extension`` option (``py`` by default) and the files whose names are passed
  813. with the ``--name`` option. The :class:`template context
  814. <django.template.Context>` used is:
  815. - Any option passed to the ``startproject`` command (among the command's
  816. supported options)
  817. - ``project_name`` -- the project name as passed to the command
  818. - ``project_directory`` -- the full path of the newly created project
  819. - ``secret_key`` -- a random key for the :setting:`SECRET_KEY` setting
  820. - ``docs_version`` -- the version of the documentation: ``'dev'`` or ``'1.x'``
  821. Please also see the :ref:`rendering warning <render_warning>` as mentioned
  822. for :djadmin:`startapp`.
  823. .. _`template source`: https://github.com/django/django/tree/master/django/conf/project_template/
  824. test <app or test identifier>
  825. -----------------------------
  826. .. django-admin:: test
  827. Runs tests for all installed models. See :doc:`/topics/testing/index` for more
  828. information.
  829. .. django-admin-option:: --failfast
  830. The ``--failfast`` option can be used to stop running tests and report the
  831. failure immediately after a test fails.
  832. .. django-admin-option:: --testrunner
  833. The ``--testrunner`` option can be used to control the test runner class that
  834. is used to execute tests. If this value is provided, it overrides the value
  835. provided by the :setting:`TEST_RUNNER` setting.
  836. .. django-admin-option:: --liveserver
  837. The ``--liveserver`` option can be used to override the default address where
  838. the live server (used with :class:`~django.test.LiveServerTestCase`) is
  839. expected to run from. The default value is ``localhost:8081-8179``.
  840. .. versionchanged:: 1.9
  841. In earlier versions, the default value was ``localhost:8081``.
  842. .. django-admin-option:: --keepdb
  843. The ``--keepdb`` option can be used to preserve the test database between test
  844. runs. This has the advantage of skipping both the create and destroy actions
  845. which can greatly decrease the time to run tests, especially those in a large
  846. test suite. If the test database does not exist, it will be created on the first
  847. run and then preserved for each subsequent run. Any unapplied migrations will also
  848. be applied to the test database before running the test suite.
  849. .. django-admin-option:: --reverse
  850. The ``--reverse`` option can be used to sort test cases in the opposite order.
  851. This may help in debugging the side effects of tests that aren't properly
  852. isolated. :ref:`Grouping by test class <order-of-tests>` is preserved when using
  853. this option.
  854. .. django-admin-option:: --debug-sql
  855. The ``--debug-sql`` option can be used to enable :ref:`SQL logging
  856. <django-db-logger>` for failing tests. If :djadminopt:`--verbosity` is ``2``,
  857. then queries in passing tests are also output.
  858. .. django-admin-option:: --parallel
  859. .. versionadded:: 1.9
  860. The ``--parallel`` option can be used to run tests in parallel in separate
  861. processes. Since modern processors have multiple cores, this allows running
  862. tests significantly faster.
  863. By default ``--parallel`` runs one process per core according to
  864. :func:`multiprocessing.cpu_count()`. You can adjust the number of processes
  865. either by providing it as the option's value, e.g. ``--parallel=4``, or by
  866. setting the ``DJANGO_TEST_PROCESSES`` environment variable.
  867. Django distributes test cases — :class:`unittest.TestCase` subclasses — to
  868. subprocesses. If there are fewer test cases than configured processes, Django
  869. will reduce the number of processes accordingly.
  870. Each process gets its own database. You must ensure that different test cases
  871. don't access the same resources. For instance, test cases that touch the
  872. filesystem should create a temporary directory for their own use.
  873. This option requires the third-party ``tblib`` package to display tracebacks
  874. correctly:
  875. .. code-block:: console
  876. $ pip install tblib
  877. This feature isn't available on Windows. It doesn't work with the Oracle
  878. database backend either.
  879. If you want to use :mod:`pdb` while debugging tests, you must disable parallel
  880. execution (``--parallel=1``). You'll see something like ``bdb.BdbQuit`` if you
  881. don't.
  882. .. warning::
  883. When test parallelization is enabled and a test fails, Django may be
  884. unable to display the exception traceback. This can make debugging
  885. difficult. If you encounter this problem, run the affected test without
  886. parallelization to see the traceback of the failure.
  887. This is a known limitation. It arises from the need to serialize objects
  888. in order to exchange them between processes. See
  889. :ref:`python:pickle-picklable` for details.
  890. testserver <fixture fixture ...>
  891. --------------------------------
  892. .. django-admin:: testserver
  893. Runs a Django development server (as in :djadmin:`runserver`) using data from
  894. the given fixture(s).
  895. For example, this command::
  896. django-admin testserver mydata.json
  897. ...would perform the following steps:
  898. 1. Create a test database, as described in :ref:`the-test-database`.
  899. 2. Populate the test database with fixture data from the given fixtures.
  900. (For more on fixtures, see the documentation for :djadmin:`loaddata` above.)
  901. 3. Runs the Django development server (as in :djadmin:`runserver`), pointed at
  902. this newly created test database instead of your production database.
  903. This is useful in a number of ways:
  904. * When you're writing :doc:`unit tests </topics/testing/overview>` of how your views
  905. act with certain fixture data, you can use ``testserver`` to interact with
  906. the views in a Web browser, manually.
  907. * Let's say you're developing your Django application and have a "pristine"
  908. copy of a database that you'd like to interact with. You can dump your
  909. database to a fixture (using the :djadmin:`dumpdata` command, explained
  910. above), then use ``testserver`` to run your Web application with that data.
  911. With this arrangement, you have the flexibility of messing up your data
  912. in any way, knowing that whatever data changes you're making are only
  913. being made to a test database.
  914. Note that this server does *not* automatically detect changes to your Python
  915. source code (as :djadmin:`runserver` does). It does, however, detect changes to
  916. templates.
  917. .. django-admin-option:: --addrport [port number or ipaddr:port]
  918. Use ``--addrport`` to specify a different port, or IP address and port, from
  919. the default of ``127.0.0.1:8000``. This value follows exactly the same format and
  920. serves exactly the same function as the argument to the :djadmin:`runserver`
  921. command.
  922. Examples:
  923. To run the test server on port 7000 with ``fixture1`` and ``fixture2``::
  924. django-admin testserver --addrport 7000 fixture1 fixture2
  925. django-admin testserver fixture1 fixture2 --addrport 7000
  926. (The above statements are equivalent. We include both of them to demonstrate
  927. that it doesn't matter whether the options come before or after the fixture
  928. arguments.)
  929. To run on 1.2.3.4:7000 with a ``test`` fixture::
  930. django-admin testserver --addrport 1.2.3.4:7000 test
  931. The :djadminopt:`--noinput` option may be provided to suppress all user
  932. prompts.
  933. Commands provided by applications
  934. =================================
  935. Some commands are only available when the ``django.contrib`` application that
  936. :doc:`implements </howto/custom-management-commands>` them has been
  937. :setting:`enabled <INSTALLED_APPS>`. This section describes them grouped by
  938. their application.
  939. ``django.contrib.auth``
  940. -----------------------
  941. changepassword
  942. ~~~~~~~~~~~~~~
  943. .. django-admin:: changepassword
  944. This command is only available if Django's :doc:`authentication system
  945. </topics/auth/index>` (``django.contrib.auth``) is installed.
  946. Allows changing a user's password. It prompts you to enter a new password twice
  947. for the given user. If the entries are identical, this immediately becomes the
  948. new password. If you do not supply a user, the command will attempt to change
  949. the password whose username matches the current user.
  950. Use the ``--database`` option to specify the database to query for the user. If
  951. it's not supplied, Django will use the ``default`` database.
  952. Example usage::
  953. django-admin changepassword ringo
  954. createsuperuser
  955. ~~~~~~~~~~~~~~~
  956. .. django-admin:: createsuperuser
  957. This command is only available if Django's :doc:`authentication system
  958. </topics/auth/index>` (``django.contrib.auth``) is installed.
  959. Creates a superuser account (a user who has all permissions). This is
  960. useful if you need to create an initial superuser account or if you need to
  961. programmatically generate superuser accounts for your site(s).
  962. When run interactively, this command will prompt for a password for
  963. the new superuser account. When run non-interactively, no password
  964. will be set, and the superuser account will not be able to log in until
  965. a password has been manually set for it.
  966. .. django-admin-option:: --username
  967. .. django-admin-option:: --email
  968. The username and email address for the new account can be supplied by
  969. using the ``--username`` and ``--email`` arguments on the command
  970. line. If either of those is not supplied, ``createsuperuser`` will prompt for
  971. it when running interactively.
  972. Use the ``--database`` option to specify the database into which the superuser
  973. object will be saved.
  974. You can subclass the management command and override ``get_input_data()`` if you
  975. want to customize data input and validation. Consult the source code for
  976. details on the existing implementation and the method's parameters. For example,
  977. it could be useful if you have a ``ForeignKey`` in
  978. :attr:`~django.contrib.auth.models.CustomUser.REQUIRED_FIELDS` and want to
  979. allow creating an instance instead of entering the primary key of an existing
  980. instance.
  981. ``django.contrib.gis``
  982. ----------------------
  983. ogrinspect
  984. ~~~~~~~~~~
  985. This command is only available if :doc:`GeoDjango </ref/contrib/gis/index>`
  986. (``django.contrib.gis``) is installed.
  987. Please refer to its :djadmin:`description <ogrinspect>` in the GeoDjango
  988. documentation.
  989. ``django.contrib.sessions``
  990. ---------------------------
  991. clearsessions
  992. ~~~~~~~~~~~~~~~
  993. .. django-admin:: clearsessions
  994. Can be run as a cron job or directly to clean out expired sessions.
  995. ``django.contrib.sitemaps``
  996. ---------------------------
  997. ping_google
  998. ~~~~~~~~~~~
  999. This command is only available if the :doc:`Sitemaps framework
  1000. </ref/contrib/sitemaps>` (``django.contrib.sitemaps``) is installed.
  1001. Please refer to its :djadmin:`description <ping_google>` in the Sitemaps
  1002. documentation.
  1003. ``django.contrib.staticfiles``
  1004. ------------------------------
  1005. collectstatic
  1006. ~~~~~~~~~~~~~
  1007. This command is only available if the :doc:`static files application
  1008. </howto/static-files/index>` (``django.contrib.staticfiles``) is installed.
  1009. Please refer to its :djadmin:`description <collectstatic>` in the
  1010. :doc:`staticfiles </ref/contrib/staticfiles>` documentation.
  1011. findstatic
  1012. ~~~~~~~~~~
  1013. This command is only available if the :doc:`static files application
  1014. </howto/static-files/index>` (``django.contrib.staticfiles``) is installed.
  1015. Please refer to its :djadmin:`description <findstatic>` in the :doc:`staticfiles
  1016. </ref/contrib/staticfiles>` documentation.
  1017. Default options
  1018. ===============
  1019. Although some commands may allow their own custom options, every command
  1020. allows for the following options:
  1021. .. django-admin-option:: --pythonpath
  1022. Example usage::
  1023. django-admin migrate --pythonpath='/home/djangoprojects/myproject'
  1024. Adds the given filesystem path to the Python `import search path`_. If this
  1025. isn't provided, ``django-admin`` will use the ``PYTHONPATH`` environment
  1026. variable.
  1027. Note that this option is unnecessary in ``manage.py``, because it takes care of
  1028. setting the Python path for you.
  1029. .. _import search path: http://www.diveintopython.net/getting_to_know_python/everything_is_an_object.html
  1030. .. django-admin-option:: --settings
  1031. Example usage::
  1032. django-admin migrate --settings=mysite.settings
  1033. Explicitly specifies the settings module to use. The settings module should be
  1034. in Python package syntax, e.g. ``mysite.settings``. If this isn't provided,
  1035. ``django-admin`` will use the ``DJANGO_SETTINGS_MODULE`` environment
  1036. variable.
  1037. Note that this option is unnecessary in ``manage.py``, because it uses
  1038. ``settings.py`` from the current project by default.
  1039. .. django-admin-option:: --traceback
  1040. Example usage::
  1041. django-admin migrate --traceback
  1042. By default, ``django-admin`` will show a simple error message whenever a
  1043. :class:`~django.core.management.CommandError` occurs, but a full stack trace
  1044. for any other exception. If you specify ``--traceback``, ``django-admin``
  1045. will also output a full stack trace when a ``CommandError`` is raised.
  1046. .. django-admin-option:: --verbosity
  1047. Example usage::
  1048. django-admin migrate --verbosity 2
  1049. Use ``--verbosity`` to specify the amount of notification and debug information
  1050. that ``django-admin`` should print to the console.
  1051. * ``0`` means no output.
  1052. * ``1`` means normal output (default).
  1053. * ``2`` means verbose output.
  1054. * ``3`` means *very* verbose output.
  1055. .. django-admin-option:: --no-color
  1056. Example usage::
  1057. django-admin runserver --no-color
  1058. By default, ``django-admin`` will format the output to be colorized. For
  1059. example, errors will be printed to the console in red and SQL statements will
  1060. be syntax highlighted. To prevent this and have a plain text output, pass the
  1061. ``--no-color`` option when running your command.
  1062. Common options
  1063. ==============
  1064. The following options are not available on every command, but they are common
  1065. to a number of commands.
  1066. .. django-admin-option:: --database
  1067. Used to specify the database on which a command will operate. If not
  1068. specified, this option will default to an alias of ``default``.
  1069. For example, to dump data from the database with the alias ``master``::
  1070. django-admin dumpdata --database=master
  1071. .. django-admin-option:: --exclude
  1072. Exclude a specific application from the applications whose contents is
  1073. output. For example, to specifically exclude the ``auth`` application from
  1074. the output of dumpdata, you would call::
  1075. django-admin dumpdata --exclude=auth
  1076. If you want to exclude multiple applications, use multiple ``--exclude``
  1077. directives::
  1078. django-admin dumpdata --exclude=auth --exclude=contenttypes
  1079. .. django-admin-option:: --locale
  1080. Use the ``--locale`` or ``-l`` option to specify the locale to process.
  1081. If not provided all locales are processed.
  1082. .. django-admin-option:: --noinput
  1083. Use the ``--noinput`` option to suppress all user prompting, such as "Are
  1084. you sure?" confirmation messages. This is useful if ``django-admin`` is
  1085. being executed as an unattended, automated script. You can use ``--no-input``
  1086. as an alias for this option.
  1087. .. versionchanged:: 1.9
  1088. The ``--no-input`` alias was added.
  1089. Extra niceties
  1090. ==============
  1091. .. _syntax-coloring:
  1092. Syntax coloring
  1093. ---------------
  1094. The ``django-admin`` / ``manage.py`` commands will use pretty
  1095. color-coded output if your terminal supports ANSI-colored output. It
  1096. won't use the color codes if you're piping the command's output to
  1097. another program.
  1098. Under Windows, the native console doesn't support ANSI escape sequences so by
  1099. default there is no color output. But you can install the `ANSICON`_
  1100. third-party tool, the Django commands will detect its presence and will make
  1101. use of its services to color output just like on Unix-based platforms.
  1102. The colors used for syntax highlighting can be customized. Django
  1103. ships with three color palettes:
  1104. * ``dark``, suited to terminals that show white text on a black
  1105. background. This is the default palette.
  1106. * ``light``, suited to terminals that show black text on a white
  1107. background.
  1108. * ``nocolor``, which disables syntax highlighting.
  1109. You select a palette by setting a ``DJANGO_COLORS`` environment
  1110. variable to specify the palette you want to use. For example, to
  1111. specify the ``light`` palette under a Unix or OS/X BASH shell, you
  1112. would run the following at a command prompt::
  1113. export DJANGO_COLORS="light"
  1114. You can also customize the colors that are used. Django specifies a
  1115. number of roles in which color is used:
  1116. * ``error`` - A major error.
  1117. * ``notice`` - A minor error.
  1118. * ``success`` - A success.
  1119. * ``warning`` - A warning.
  1120. * ``sql_field`` - The name of a model field in SQL.
  1121. * ``sql_coltype`` - The type of a model field in SQL.
  1122. * ``sql_keyword`` - An SQL keyword.
  1123. * ``sql_table`` - The name of a model in SQL.
  1124. * ``http_info`` - A 1XX HTTP Informational server response.
  1125. * ``http_success`` - A 2XX HTTP Success server response.
  1126. * ``http_not_modified`` - A 304 HTTP Not Modified server response.
  1127. * ``http_redirect`` - A 3XX HTTP Redirect server response other than 304.
  1128. * ``http_not_found`` - A 404 HTTP Not Found server response.
  1129. * ``http_bad_request`` - A 4XX HTTP Bad Request server response other than 404.
  1130. * ``http_server_error`` - A 5XX HTTP Server Error response.
  1131. * ``migrate_heading`` - A heading in a migrations management command.
  1132. * ``migrate_label`` - A migration name.
  1133. .. versionchanged:: 1.9
  1134. ``success`` was added.
  1135. Each of these roles can be assigned a specific foreground and
  1136. background color, from the following list:
  1137. * ``black``
  1138. * ``red``
  1139. * ``green``
  1140. * ``yellow``
  1141. * ``blue``
  1142. * ``magenta``
  1143. * ``cyan``
  1144. * ``white``
  1145. Each of these colors can then be modified by using the following
  1146. display options:
  1147. * ``bold``
  1148. * ``underscore``
  1149. * ``blink``
  1150. * ``reverse``
  1151. * ``conceal``
  1152. A color specification follows one of the following patterns:
  1153. * ``role=fg``
  1154. * ``role=fg/bg``
  1155. * ``role=fg,option,option``
  1156. * ``role=fg/bg,option,option``
  1157. where ``role`` is the name of a valid color role, ``fg`` is the
  1158. foreground color, ``bg`` is the background color and each ``option``
  1159. is one of the color modifying options. Multiple color specifications
  1160. are then separated by a semicolon. For example::
  1161. export DJANGO_COLORS="error=yellow/blue,blink;notice=magenta"
  1162. would specify that errors be displayed using blinking yellow on blue,
  1163. and notices displayed using magenta. All other color roles would be
  1164. left uncolored.
  1165. Colors can also be specified by extending a base palette. If you put
  1166. a palette name in a color specification, all the colors implied by that
  1167. palette will be loaded. So::
  1168. export DJANGO_COLORS="light;error=yellow/blue,blink;notice=magenta"
  1169. would specify the use of all the colors in the light color palette,
  1170. *except* for the colors for errors and notices which would be
  1171. overridden as specified.
  1172. .. _ANSICON: http://adoxa.altervista.org/ansicon/
  1173. Bash completion
  1174. ---------------
  1175. If you use the Bash shell, consider installing the Django bash completion
  1176. script, which lives in ``extras/django_bash_completion`` in the Django
  1177. distribution. It enables tab-completion of ``django-admin`` and
  1178. ``manage.py`` commands, so you can, for instance...
  1179. * Type ``django-admin``.
  1180. * Press [TAB] to see all available options.
  1181. * Type ``sql``, then [TAB], to see all available options whose names start
  1182. with ``sql``.
  1183. See :doc:`/howto/custom-management-commands` for how to add customized actions.
  1184. ==========================================
  1185. Running management commands from your code
  1186. ==========================================
  1187. .. _call-command:
  1188. .. function:: django.core.management.call_command(name, *args, **options)
  1189. To call a management command from code use ``call_command``.
  1190. ``name``
  1191. the name of the command to call.
  1192. ``*args``
  1193. a list of arguments accepted by the command.
  1194. ``**options``
  1195. named options accepted on the command-line.
  1196. Examples::
  1197. from django.core import management
  1198. management.call_command('flush', verbosity=0, interactive=False)
  1199. management.call_command('loaddata', 'test_data', verbosity=0)
  1200. Note that command options that take no arguments are passed as keywords
  1201. with ``True`` or ``False``, as you can see with the ``interactive`` option above.
  1202. Named arguments can be passed by using either one of the following syntaxes::
  1203. # Similar to the command line
  1204. management.call_command('dumpdata', '--natural-foreign')
  1205. # Named argument similar to the command line minus the initial dashes and
  1206. # with internal dashes replaced by underscores
  1207. management.call_command('dumpdata', natural_foreign=True)
  1208. # `use_natural_foreign_keys` is the option destination variable
  1209. management.call_command('dumpdata', use_natural_foreign_keys=True)
  1210. Command options which take multiple options are passed a list::
  1211. management.call_command('dumpdata', exclude=['contenttypes', 'auth'])
  1212. Output redirection
  1213. ==================
  1214. Note that you can redirect standard output and error streams as all commands
  1215. support the ``stdout`` and ``stderr`` options. For example, you could write::
  1216. with open('/tmp/command_output') as f:
  1217. management.call_command('dumpdata', stdout=f)