2
0

django-admin.txt 71 KB

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