django-admin.txt 72 KB

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