django-admin.txt 62 KB

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