django-admin.txt 59 KB

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