Django

Code

root/django/branches/sqlalchemy/docs/django-admin.txt

Revision 4455, 13.9 kB (checked in by rmunn, 2 years ago)

Merged revisions 4186 to 4454 from trunk.

Line 
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: ../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: ../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: ../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 Note that the order in which the SQL files are processed is undefined.
257
258 sqlreset [appname appname ...]
259 --------------------------------------
260
261 Prints the DROP TABLE SQL, then the CREATE TABLE SQL, for the given appnames.
262
263 sqlsequencereset [appname appname ...]
264 ----------------------------------------------
265
266 Prints the SQL statements for resetting PostgreSQL sequences for the given
267 appnames.
268
269 See http://simon.incutio.com/archive/2004/04/21/postgres for more information.
270
271 startapp [appname]
272 ------------------
273
274 Creates a Django app directory structure for the given app name in the current
275 directory.
276
277 startproject [projectname]
278 --------------------------
279
280 Creates a Django project directory structure for the given project name in the
281 current directory.
282
283 syncdb
284 ------
285
286 Creates the database tables for all apps in ``INSTALLED_APPS`` whose tables
287 have not already been created.
288
289 Use this command when you've added new applications to your project and want to
290 install them in the database. This includes any apps shipped with Django that
291 might be in ``INSTALLED_APPS`` by default. When you start a new project, run
292 this command to install the default apps.
293
294 If you're installing the ``django.contrib.auth`` application, ``syncdb`` will
295 give you the option of creating a superuser immediately.
296
297 test
298 ----
299
300 **New in Django development version**
301
302 Discover and run tests for all installed models.  See `Testing Django applications`_ for more information.
303
304 .. _testing django applications: ../testing/
305
306 validate
307 --------
308
309 Validates all installed models (according to the ``INSTALLED_APPS`` setting)
310 and prints validation errors to standard output.
311
312 Available options
313 =================
314
315 --settings
316 ----------
317
318 Example usage::
319
320     django-admin.py syncdb --settings=mysite.settings
321
322 Explicitly specifies the settings module to use. The settings module should be
323 in Python package syntax, e.g. ``mysite.settings``. If this isn't provided,
324 ``django-admin.py`` will use the ``DJANGO_SETTINGS_MODULE`` environment
325 variable.
326
327 Note that this option is unnecessary in ``manage.py``, because it takes care of
328 setting ``DJANGO_SETTINGS_MODULE`` for you.
329
330 --pythonpath
331 ------------
332
333 Example usage::
334
335     django-admin.py syncdb --pythonpath='/home/djangoprojects/myproject'
336
337 Adds the given filesystem path to the Python `import search path`_. If this
338 isn't provided, ``django-admin.py`` will use the ``PYTHONPATH`` environment
339 variable.
340
341 Note that this option is unnecessary in ``manage.py``, because it takes care of
342 setting the Python path for you.
343
344 .. _import search path: http://diveintopython.org/getting_to_know_python/everything_is_an_object.html
345
346 --help
347 ------
348
349 Displays a help message that includes a terse list of all available actions and
350 options.
351
352 --noinput
353 ---------
354
355 **New in Django development version**
356
357 Inform django-admin that the user should NOT be prompted for any input. Useful
358 if the django-admin script will be executed as an unattended, automated
359 script.
360
361 --noreload
362 ----------
363
364 Disable the use of the auto-reloader when running the development server.
365
366 --version
367 ---------
368
369 Displays the current Django version.
370
371 Example output::
372
373     0.9.1
374     0.9.1 (SVN)
375
376 --verbosity
377 -----------
378
379 **New in Django development version**
380
381 Example usage::
382
383     django-admin.py syncdb --verbosity=2
384
385 Verbosity determines the amount of notification and debug information that
386 will be printed to the console. '0' is no output, '1' is normal output,
387 and `2` is verbose output.
388
389 --adminmedia
390 ------------
391
392 **New in Django development version**
393
394 Example usage::
395     django-admin.py manage.py --adminmedia=/tmp/new-admin-style/
396
397 Tells Django where to find the various CSS and JavaScript files for the admin
398 interface when running the development server. Normally these files are served
399 out of the Django source tree, but because some designers customize these files
400 for their site, this option allows you to test against custom versions.
401
402 Extra niceties
403 ==============
404
405 Syntax coloring
406 ---------------
407
408 The ``django-admin.py`` / ``manage.py`` commands that output SQL to standard
409 output will use pretty color-coded output if your terminal supports
410 ANSI-colored output. It won't use the color codes if you're piping the
411 command's output to another program.
412
413 Bash completion
414 ---------------
415
416 If you use the Bash shell, consider installing the Django bash completion
417 script, which lives in ``extras/django_bash_completion`` in the Django
418 distribution. It enables tab-completion of ``django-admin.py`` and
419 ``manage.py`` commands, so you can, for instance...
420
421     * Type ``django-admin.py``.
422     * Press [TAB] to see all available options.
423     * Type ``sql``, then [TAB], to see all available options whose names start
424       with ``sql``.
Note: See TracBrowser for help on using the browser.