django-admin.txt 62 KB

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