| 1 |
============================= |
|---|
| 2 |
django-admin.py and manage.py |
|---|
| 3 |
============================= |
|---|
| 4 |
|
|---|
| 5 |
``django-admin.py`` is Django's command-line utility for administrative tasks. |
|---|
| 6 |
This document outlines all it can do. |
|---|
| 7 |
|
|---|
| 8 |
In addition, ``manage.py`` is automatically created in each Django project. |
|---|
| 9 |
``manage.py`` is a thin wrapper around ``django-admin.py`` that takes care of |
|---|
| 10 |
two things for you before delegating to ``django-admin.py``: |
|---|
| 11 |
|
|---|
| 12 |
* It puts your project's package on ``sys.path``. |
|---|
| 13 |
|
|---|
| 14 |
* It sets the ``DJANGO_SETTINGS_MODULE`` environment variable so that it |
|---|
| 15 |
points to your project's ``settings.py`` file. |
|---|
| 16 |
|
|---|
| 17 |
The ``django-admin.py`` script should be on your system path if you installed |
|---|
| 18 |
Django via its ``setup.py`` utility. If it's not on your path, you can find it in |
|---|
| 19 |
``site-packages/django/bin`` within your Python installation. Consider |
|---|
| 20 |
symlinking to it from some place on your path, such as ``/usr/local/bin``. |
|---|
| 21 |
|
|---|
| 22 |
Generally, when working on a single Django project, it's easier to use |
|---|
| 23 |
``manage.py``. Use ``django-admin.py`` with ``DJANGO_SETTINGS_MODULE``, or the |
|---|
| 24 |
``--settings`` command line option, if you need to switch between multiple |
|---|
| 25 |
Django settings files. |
|---|
| 26 |
|
|---|
| 27 |
Usage |
|---|
| 28 |
===== |
|---|
| 29 |
|
|---|
| 30 |
``django-admin.py action [options]`` |
|---|
| 31 |
|
|---|
| 32 |
``manage.py action [options]`` |
|---|
| 33 |
|
|---|
| 34 |
``action`` should be one of the actions listed in this document. ``options``, |
|---|
| 35 |
which is optional, should be zero or more of the options listed in this |
|---|
| 36 |
document. |
|---|
| 37 |
|
|---|
| 38 |
Run ``django-admin.py --help`` to display a help message that includes a terse |
|---|
| 39 |
list of all available actions and options. |
|---|
| 40 |
|
|---|
| 41 |
Most actions take a list of ``appname``s. An ``appname`` is the basename of the |
|---|
| 42 |
package containing your models. For example, if your ``INSTALLED_APPS`` |
|---|
| 43 |
contains the string ``'mysite.blog'``, the ``appname`` is ``blog``. |
|---|
| 44 |
|
|---|
| 45 |
Available actions |
|---|
| 46 |
================= |
|---|
| 47 |
|
|---|
| 48 |
adminindex [appname appname ...] |
|---|
| 49 |
-------------------------------- |
|---|
| 50 |
|
|---|
| 51 |
Prints the admin-index template snippet for the given appnames. |
|---|
| 52 |
|
|---|
| 53 |
Use admin-index template snippets if you want to customize the look and feel of |
|---|
| 54 |
your admin's index page. See `Tutorial 2`_ for more information. |
|---|
| 55 |
|
|---|
| 56 |
.. _Tutorial 2: http://www.djangoproject.com/documentation/tutorial2/ |
|---|
| 57 |
|
|---|
| 58 |
createcachetable [tablename] |
|---|
| 59 |
---------------------------- |
|---|
| 60 |
|
|---|
| 61 |
Creates a cache table named ``tablename`` for use with the database cache |
|---|
| 62 |
backend. See the `cache documentation`_ for more information. |
|---|
| 63 |
|
|---|
| 64 |
.. _cache documentation: http://www.djangoproject.com/documentation/cache/ |
|---|
| 65 |
|
|---|
| 66 |
dbshell |
|---|
| 67 |
------- |
|---|
| 68 |
|
|---|
| 69 |
Runs the command-line client for the database engine specified in your |
|---|
| 70 |
``DATABASE_ENGINE`` setting, with the connection parameters specified in your |
|---|
| 71 |
``DATABASE_USER``, ``DATABASE_PASSWORD``, etc., settings. |
|---|
| 72 |
|
|---|
| 73 |
* For PostgreSQL, this runs the ``psql`` command-line client. |
|---|
| 74 |
* For MySQL, this runs the ``mysql`` command-line client. |
|---|
| 75 |
* For SQLite, this runs the ``sqlite3`` command-line client. |
|---|
| 76 |
|
|---|
| 77 |
This command assumes the programs are on your ``PATH`` so that a simple call to |
|---|
| 78 |
the program name (``psql``, ``mysql``, ``sqlite3``) will find the program in |
|---|
| 79 |
the right place. There's no way to specify the location of the program |
|---|
| 80 |
manually. |
|---|
| 81 |
|
|---|
| 82 |
diffsettings |
|---|
| 83 |
------------ |
|---|
| 84 |
|
|---|
| 85 |
Displays differences between the current settings file and Django's default |
|---|
| 86 |
settings. |
|---|
| 87 |
|
|---|
| 88 |
Settings that don't appear in the defaults are followed by ``"###"``. For |
|---|
| 89 |
example, the default settings don't define ``ROOT_URLCONF``, so |
|---|
| 90 |
``ROOT_URLCONF`` is followed by ``"###"`` in the output of ``diffsettings``. |
|---|
| 91 |
|
|---|
| 92 |
Note that Django's default settings live in ``django/conf/global_settings.py``, |
|---|
| 93 |
if you're ever curious to see the full list of defaults. |
|---|
| 94 |
|
|---|
| 95 |
inspectdb |
|---|
| 96 |
--------- |
|---|
| 97 |
|
|---|
| 98 |
Introspects the database tables in the database pointed-to by the |
|---|
| 99 |
``DATABASE_NAME`` setting and outputs a Django model module (a ``models.py`` |
|---|
| 100 |
file) to standard output. |
|---|
| 101 |
|
|---|
| 102 |
Use this if you have a legacy database with which you'd like to use Django. |
|---|
| 103 |
The script will inspect the database and create a model for each table within |
|---|
| 104 |
it. |
|---|
| 105 |
|
|---|
| 106 |
As you might expect, the created models will have an attribute for every field |
|---|
| 107 |
in the table. Note that ``inspectdb`` has a few special cases in its field-name |
|---|
| 108 |
output: |
|---|
| 109 |
|
|---|
| 110 |
* If ``inspectdb`` cannot map a column's type to a model field type, it'll |
|---|
| 111 |
use ``TextField`` and will insert the Python comment |
|---|
| 112 |
``'This field type is a guess.'`` next to the field in the generated |
|---|
| 113 |
model. |
|---|
| 114 |
|
|---|
| 115 |
* If the database column name is a Python reserved word (such as |
|---|
| 116 |
``'pass'``, ``'class'`` or ``'for'``), ``inspectdb`` will append |
|---|
| 117 |
``'_field'`` to the attribute name. For example, if a table has a column |
|---|
| 118 |
``'for'``, the generated model will have a field ``'for_field'``, with |
|---|
| 119 |
the ``db_column`` attribute set to ``'for'``. ``inspectdb`` will insert |
|---|
| 120 |
the Python comment |
|---|
| 121 |
``'Field renamed because it was a Python reserved word.'`` next to the |
|---|
| 122 |
field. |
|---|
| 123 |
|
|---|
| 124 |
This feature is meant as a shortcut, not as definitive model generation. After |
|---|
| 125 |
you run it, you'll want to look over the generated models yourself to make |
|---|
| 126 |
customizations. In particular, you'll need to rearrange models' order, so that |
|---|
| 127 |
models that refer to other models are ordered properly. |
|---|
| 128 |
|
|---|
| 129 |
Primary keys are automatically introspected for PostgreSQL, MySQL and |
|---|
| 130 |
SQLite, in which case Django puts in the ``primary_key=True`` where |
|---|
| 131 |
needed. |
|---|
| 132 |
|
|---|
| 133 |
``inspectdb`` works with PostgreSQL, MySQL and SQLite. Foreign-key detection |
|---|
| 134 |
only works in PostgreSQL and with certain types of MySQL tables. |
|---|
| 135 |
|
|---|
| 136 |
install [appname appname ...] |
|---|
| 137 |
----------------------------- |
|---|
| 138 |
|
|---|
| 139 |
Executes the equivalent of ``sqlall`` for the given appnames. |
|---|
| 140 |
|
|---|
| 141 |
runserver [optional port number, or ipaddr:port] |
|---|
| 142 |
------------------------------------------------ |
|---|
| 143 |
|
|---|
| 144 |
Starts a lightweight development Web server on the local machine. By default, |
|---|
| 145 |
the server runs on port 8000 on the IP address 127.0.0.1. You can pass in an |
|---|
| 146 |
IP address and port number explicitly. |
|---|
| 147 |
|
|---|
| 148 |
If you run this script as a user with normal privileges (recommended), you |
|---|
| 149 |
might not have access to start a port on a low port number. Low port numbers |
|---|
| 150 |
are reserved for the superuser (root). |
|---|
| 151 |
|
|---|
| 152 |
DO NOT USE THIS SERVER IN A PRODUCTION SETTING. It has not gone through |
|---|
| 153 |
security audits or performance tests. (And that's how it's gonna stay. We're in |
|---|
| 154 |
the business of making Web frameworks, not Web servers, so improving this |
|---|
| 155 |
server to be able to handle a production environment is outside the scope of |
|---|
| 156 |
Django.) |
|---|
| 157 |
|
|---|
| 158 |
The development server automatically reloads Python code for each request, as |
|---|
| 159 |
needed. You don't need to restart the server for code changes to take effect. |
|---|
| 160 |
|
|---|
| 161 |
When you start the server, and each time you change Python code while the |
|---|
| 162 |
server is running, the server will validate all of your installed models. (See |
|---|
| 163 |
the ``validate`` command below.) If the validator finds errors, it will print |
|---|
| 164 |
them to standard output, but it won't stop the server. |
|---|
| 165 |
|
|---|
| 166 |
You can run as many servers as you want, as long as they're on separate ports. |
|---|
| 167 |
Just execute ``django-admin.py runserver`` more than once. |
|---|
| 168 |
|
|---|
| 169 |
Note that the default IP address, 127.0.0.1, is not accessible from other |
|---|
| 170 |
machines on your network. To make your development server viewable to other |
|---|
| 171 |
machines on the network, use its own IP address (e.g. ``192.168.2.1``) or |
|---|
| 172 |
``0.0.0.0``. |
|---|
| 173 |
|
|---|
| 174 |
Examples: |
|---|
| 175 |
~~~~~~~~~ |
|---|
| 176 |
|
|---|
| 177 |
Port 7000 on IP address 127.0.0.1:: |
|---|
| 178 |
|
|---|
| 179 |
django-admin.py runserver 7000 |
|---|
| 180 |
|
|---|
| 181 |
Port 7000 on IP address 1.2.3.4:: |
|---|
| 182 |
|
|---|
| 183 |
django-admin.py runserver 1.2.3.4:7000 |
|---|
| 184 |
|
|---|
| 185 |
Serving static files with the development server |
|---|
| 186 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 187 |
|
|---|
| 188 |
By default, the development server doesn't serve any static files for your site |
|---|
| 189 |
(such as CSS files, images, things under ``MEDIA_ROOT_URL`` and so forth). If |
|---|
| 190 |
you want to configure Django to serve static media, read the `serving static files`_ |
|---|
| 191 |
documentation. |
|---|
| 192 |
|
|---|
| 193 |
.. _serving static files: http://www.djangoproject.com/documentation/static_files/ |
|---|
| 194 |
|
|---|
| 195 |
Turning off auto-reload |
|---|
| 196 |
~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 197 |
|
|---|
| 198 |
To disable auto-reloading of code while the development server is running, use the |
|---|
| 199 |
``--noreload`` option, like so:: |
|---|
| 200 |
|
|---|
| 201 |
django-admin.py runserver --noreload |
|---|
| 202 |
|
|---|
| 203 |
shell |
|---|
| 204 |
----- |
|---|
| 205 |
|
|---|
| 206 |
Starts the Python interactive interpreter. |
|---|
| 207 |
|
|---|
| 208 |
Django will use IPython_, if it's installed. If you have IPython installed and |
|---|
| 209 |
want to force use of the "plain" Python interpreter, use the ``--plain`` |
|---|
| 210 |
option, like so:: |
|---|
| 211 |
|
|---|
| 212 |
django-admin.py shell --plain |
|---|
| 213 |
|
|---|
| 214 |
.. _IPython: http://ipython.scipy.org/ |
|---|
| 215 |
|
|---|
| 216 |
sql [appname appname ...] |
|---|
| 217 |
------------------------- |
|---|
| 218 |
|
|---|
| 219 |
Prints the CREATE TABLE SQL statements for the given appnames. |
|---|
| 220 |
|
|---|
| 221 |
sqlall [appname appname ...] |
|---|
| 222 |
---------------------------- |
|---|
| 223 |
|
|---|
| 224 |
Prints the CREATE TABLE and initial-data SQL statements for the given appnames. |
|---|
| 225 |
|
|---|
| 226 |
Refer to the description of ``sqlinitialdata`` for an explanation of how to |
|---|
| 227 |
specify initial data. |
|---|
| 228 |
|
|---|
| 229 |
sqlclear [appname appname ...] |
|---|
| 230 |
-------------------------------------- |
|---|
| 231 |
|
|---|
| 232 |
Prints the DROP TABLE SQL statements for the given appnames. |
|---|
| 233 |
|
|---|
| 234 |
sqlindexes [appname appname ...] |
|---|
| 235 |
---------------------------------------- |
|---|
| 236 |
|
|---|
| 237 |
Prints the CREATE INDEX SQL statements for the given appnames. |
|---|
| 238 |
|
|---|
| 239 |
sqlinitialdata [appname appname ...] |
|---|
| 240 |
-------------------------------------------- |
|---|
| 241 |
|
|---|
| 242 |
Prints the initial INSERT SQL statements for the given appnames. |
|---|
| 243 |
|
|---|
| 244 |
For each model in each specified app, this command looks for the file |
|---|
| 245 |
``<appname>/sql/<modelname>.sql``, where ``<appname>`` is the given appname and |
|---|
| 246 |
``<modelname>`` is the model's name in lowercase. For example, if you have an |
|---|
| 247 |
app ``news`` that includes a ``Story`` model, ``sqlinitialdata`` will attempt |
|---|
| 248 |
to read a file ``news/sql/story.sql`` and append it to the output of this |
|---|
| 249 |
command. |
|---|
| 250 |
|
|---|
| 251 |
Each of the SQL files, if given, is expected to contain valid SQL. The SQL |
|---|
| 252 |
files are piped directly into the database after all of the models' |
|---|
| 253 |
table-creation statements have been executed. Use this SQL hook to populate |
|---|
| 254 |
tables with any necessary initial records, SQL functions or test data. |
|---|
| 255 |
|
|---|
| 256 |
sqlreset [appname appname ...] |
|---|
| 257 |
-------------------------------------- |
|---|
| 258 |
|
|---|
| 259 |
Prints the DROP TABLE SQL, then the CREATE TABLE SQL, for the given appnames. |
|---|
| 260 |
|
|---|
| 261 |
sqlsequencereset [appname appname ...] |
|---|
| 262 |
---------------------------------------------- |
|---|
| 263 |
|
|---|
| 264 |
Prints the SQL statements for resetting PostgreSQL sequences for the given |
|---|
| 265 |
appnames. |
|---|
| 266 |
|
|---|
| 267 |
See http://simon.incutio.com/archive/2004/04/21/postgres for more information. |
|---|
| 268 |
|
|---|
| 269 |
startapp [appname] |
|---|
| 270 |
------------------ |
|---|
| 271 |
|
|---|
| 272 |
Creates a Django app directory structure for the given app name in the current |
|---|
| 273 |
directory. |
|---|
| 274 |
|
|---|
| 275 |
startproject [projectname] |
|---|
| 276 |
-------------------------- |
|---|
| 277 |
|
|---|
| 278 |
Creates a Django project directory structure for the given project name in the |
|---|
| 279 |
current directory. |
|---|
| 280 |
|
|---|
| 281 |
syncdb |
|---|
| 282 |
------ |
|---|
| 283 |
|
|---|
| 284 |
Creates the database tables for all apps in ``INSTALLED_APPS`` whose tables |
|---|
| 285 |
have not already been created. |
|---|
| 286 |
|
|---|
| 287 |
Use this command when you've added new applications to your project and want to |
|---|
| 288 |
install them in the database. This includes any apps shipped with Django that |
|---|
| 289 |
might be in ``INSTALLED_APPS`` by default. When you start a new project, run |
|---|
| 290 |
this command to install the default apps. |
|---|
| 291 |
|
|---|
| 292 |
If you're installing the ``django.contrib.auth`` application, ``syncdb`` will |
|---|
| 293 |
give you the option of creating a superuser immediately. |
|---|
| 294 |
|
|---|
| 295 |
validate |
|---|
| 296 |
-------- |
|---|
| 297 |
|
|---|
| 298 |
Validates all installed models (according to the ``INSTALLED_APPS`` setting) |
|---|
| 299 |
and prints validation errors to standard output. |
|---|
| 300 |
|
|---|
| 301 |
Available options |
|---|
| 302 |
================= |
|---|
| 303 |
|
|---|
| 304 |
--settings |
|---|
| 305 |
---------- |
|---|
| 306 |
|
|---|
| 307 |
Example usage:: |
|---|
| 308 |
|
|---|
| 309 |
django-admin.py syncdb --settings=mysite.settings |
|---|
| 310 |
|
|---|
| 311 |
Explicitly specifies the settings module to use. The settings module should be |
|---|
| 312 |
in Python package syntax, e.g. ``mysite.settings``. If this isn't provided, |
|---|
| 313 |
``django-admin.py`` will use the ``DJANGO_SETTINGS_MODULE`` environment |
|---|
| 314 |
variable. |
|---|
| 315 |
|
|---|
| 316 |
Note that this option is unnecessary in ``manage.py``, because it takes care of |
|---|
| 317 |
setting ``DJANGO_SETTINGS_MODULE`` for you. |
|---|
| 318 |
|
|---|
| 319 |
--pythonpath |
|---|
| 320 |
------------ |
|---|
| 321 |
|
|---|
| 322 |
Example usage:: |
|---|
| 323 |
|
|---|
| 324 |
django-admin.py syncdb --pythonpath='/home/djangoprojects/myproject' |
|---|
| 325 |
|
|---|
| 326 |
Adds the given filesystem path to the Python `import search path`_. If this |
|---|
| 327 |
isn't provided, ``django-admin.py`` will use the ``PYTHONPATH`` environment |
|---|
| 328 |
variable. |
|---|
| 329 |
|
|---|
| 330 |
Note that this option is unnecessary in ``manage.py``, because it takes care of |
|---|
| 331 |
setting the Python path for you. |
|---|
| 332 |
|
|---|
| 333 |
.. _import search path: http://diveintopython.org/getting_to_know_python/everything_is_an_object.html |
|---|
| 334 |
|
|---|
| 335 |
--help |
|---|
| 336 |
------ |
|---|
| 337 |
|
|---|
| 338 |
Displays a help message that includes a terse list of all available actions and |
|---|
| 339 |
options. |
|---|
| 340 |
|
|---|
| 341 |
--version |
|---|
| 342 |
--------- |
|---|
| 343 |
|
|---|
| 344 |
Displays the current Django version. |
|---|
| 345 |
|
|---|
| 346 |
Example output:: |
|---|
| 347 |
|
|---|
| 348 |
0.9.1 |
|---|
| 349 |
0.9.1 (SVN) |
|---|
| 350 |
|
|---|
| 351 |
Extra niceties |
|---|
| 352 |
============== |
|---|
| 353 |
|
|---|
| 354 |
Syntax coloring |
|---|
| 355 |
--------------- |
|---|
| 356 |
|
|---|
| 357 |
The ``django-admin.py`` / ``manage.py`` commands that output SQL to standard |
|---|
| 358 |
output will use pretty color-coded output if your terminal supports |
|---|
| 359 |
ANSI-colored output. It won't use the color codes if you're piping the |
|---|
| 360 |
command's output to another program. |
|---|
| 361 |
|
|---|
| 362 |
Bash completion |
|---|
| 363 |
--------------- |
|---|
| 364 |
|
|---|
| 365 |
If you use the Bash shell, consider installing the Django bash completion |
|---|
| 366 |
script, which lives in ``extras/django_bash_completion`` in the Django |
|---|
| 367 |
distribution. It enables tab-completion of ``django-admin.py`` and |
|---|
| 368 |
``manage.py`` commands, so you can, for instance... |
|---|
| 369 |
|
|---|
| 370 |
* Type ``django-admin.py``. |
|---|
| 371 |
* Press [TAB] to see all available options. |
|---|
| 372 |
* Type ``sql``, then [TAB], to see all available options whose names start |
|---|
| 373 |
with ``sql``. |
|---|