Django

Code

root/django/branches/magic-removal/docs/faq.txt

Revision 2762, 23.6 kB (checked in by adrian, 3 years ago)

magic-removal: Proofread docs/faq.txt

Line 
1 ==========
2 Django FAQ
3 ==========
4
5 General questions
6 =================
7
8 Why does this project exist?
9 ----------------------------
10
11 Django grew from a very practical need: World Online, a newspaper Web
12 operation, is responsible for building intensive Web applications on journalism
13 deadlines. In the fast-paced newsroom, World Online often has only a matter of
14 hours to take a complicated Web application from concept to public launch.
15
16 At the same time, the World Online Web developers have consistently been
17 perfectionists when it comes to following best practices of Web development.
18
19 Thus, Django was designed not only to allow fast Web development, but
20 *best-practice* Web development.
21
22 Django would not be possible without a whole host of open-source projects --
23 `Apache`_, `Python`_, and `PostgreSQL`_ to name a few -- and we're thrilled to
24 be able to give something back to the open-source community.
25
26 .. _Apache: http://httpd.apache.org/
27 .. _Python: http://www.python.org/
28 .. _PostgreSQL: http://www.postgresql.org/
29
30 What does "Django" mean, and how do you pronounce it?
31 -----------------------------------------------------
32
33 Django is named after `Django Reinhardt`_, a gypsy jazz guitarist from the 1930s
34 to early 1950s. To this day, he's considered one of the best guitarists of all time.
35
36 Listen to his music. You'll like it.
37
38 Django is pronounced **JANG**-oh. Rhymes with FANG-oh.
39
40 .. _Django Reinhardt: http://en.wikipedia.org/wiki/Django_Reinhardt
41
42 Is Django stable?
43 -----------------
44
45 Yes. World Online has been using Django for more than two years. Sites built on
46 Django have weathered traffic spikes of over one million hits an hour and at
47 least one Slashdotting. Yes, it's quite stable.
48
49 Does Django scale?
50 ------------------
51
52 Yes. Compared to development time, hardware is cheap, and so Django is
53 designed to take advantage of as much hardware as you can throw at it.
54
55 Django uses a "shared-nothing" architecture, which means you can add hardware
56 at any level -- database servers, caching servers or Web/application servers.
57
58 The framework cleanly separates components such as its database layer and
59 application layer. And it ships with a simple-yet-powerful `cache framework`_.
60
61 .. _`cache framework`: http://www.djangoproject.com/documentation/cache/
62
63 Who's behind this?
64 ------------------
65
66 Django was developed at `World Online`_, the Web department of a newspaper in
67 Lawrence, Kansas, USA.
68
69 `Adrian Holovaty`_
70     Adrian is a Web developer with a background in journalism. He was lead
71     developer at World Online for 2.5 years, during which time Django was
72     developed and implemented on World Online's sites. Now he works for
73     washingtonpost.com building rich, database-backed information sites, and
74     continues to oversee Django development. He likes playing guitar (Django
75     Reinhardt style) and hacking on side projects such as `chicagocrime.org`_.
76     He lives in Chicago.
77
78     On IRC, Adrian goes by ``adrian_h``.
79
80 `Jacob Kaplan-Moss`_
81     Jacob is a whipper-snapper from California who spends equal time coding and
82     cooking. He's lead developer at World Online and actively hacks on various
83     cool side projects. He's contributed to the Python-ObjC bindings and was
84     the first guy to figure out how to write Tivo apps in Python. Lately he's
85     been messing with Python on the PSP. He lives in Lawrence, Kansas.
86
87     On IRC, Jacob goes by ``jacobkm``.
88
89 `Simon Willison`_
90     Simon is a well-respected Web developer from England. He had a one-year
91     internship at World Online, during which time he and Adrian developed
92     Django from scratch. The most enthusiastic Brit you'll ever meet, he's
93     passionate about best practices in Web development and has maintained a
94     well-read Web-development blog for years at http://simon.incutio.com.
95     He works for Yahoo UK, where he managed to score the title "Hacker Liason."
96     He lives in London.
97
98     On IRC, Simon goes by ``SimonW``.
99
100 `Wilson Miner`_
101     Wilson's design-fu makes us all look like rock stars. When not sneaking
102     into apartment complex swimming pools, he's the Commercial Development
103     Director for World Online, which means he makes the money that pays all our
104     paychecks. He lives in Lawrence, Kansas.
105
106     On IRC, Wilson goes by ``wilsonian``.
107
108 .. _`World Online`: http://code.djangoproject.com/wiki/WorldOnline
109 .. _`Adrian Holovaty`: http://www.holovaty.com/
110 .. _`washingtonpost.com`: http://www.washingtonpost.com/
111 .. _`chicagocrime.org`: http://www.chicagocrime.org/
112 .. _`Simon Willison`: http://simon.incutio.com/
113 .. _`simon.incutio.com`: http://simon.incutio.com/
114 .. _`Jacob Kaplan-Moss`: http://www.jacobian.org/
115 .. _`Wilson Miner`: http://www.wilsonminer.com/live/
116
117 Which sites use Django?
118 -----------------------
119
120 The Django wiki features a consistently growing `list of Django-powered sites`_.
121 Feel free to add your Django-powered site to the list.
122
123 .. _list of Django-powered sites: http://code.djangoproject.com/wiki/DjangoPoweredSites
124
125 Django appears to be a MVC framework, but you call the Controller the "view", and the View the "template". How come you don't use the standard names?
126 -----------------------------------------------------------------------------------------------------------------------------------------------------
127
128 That's because Django isn't strictly a MVC framework. If you squint the right
129 way, you can call Django's database layer the "Model", the view functions the
130 "View", and the URL dispatcher the "Controller" -- but not really.
131
132 In fact, you might say that Django is a "MTV" framework -- that is, Model,
133 Template, and View make much more sense to us.
134
135 So, although we've been strongly influenced by MVC -- especially in the
136 separation-of-data-from-logic department -- we've also strayed from the path
137 where it makes sense.
138
139 <Framework X> does <feature Y> -- why doesn't Django?
140 -----------------------------------------------------
141
142 We're well aware that there are other awesome Web frameworks out there, and
143 we're not adverse to borrowing ideas where appropriate. However, Django was
144 developed precisely because we were unhappy with the status quo, so please be
145 aware that "because <Framework X>" does it is not going to be sufficient reason
146 to add a given feature to Django.
147
148 Why did you write all of Django from scratch, instead of using other Python libraries?
149 --------------------------------------------------------------------------------------
150
151 When Django was originally written a couple of years ago, Adrian and Simon
152 spent quite a bit of time exploring the various Python Web frameworks
153 available.
154
155 In our opinion, none of them were completely up to snuff.
156
157 We're picky. You might even call us perfectionists. (With deadlines.)
158
159 Over time, we stumbled across open-source libraries that did things we'd
160 already implemented. It was reassuring to see other people solving similar
161 problems in similar ways, but it was too late to integrate outside code: We'd
162 already written, tested and implemented our own framework bits in several
163 production settings -- and our own code met our needs delightfully.
164
165 In most cases, however, we found that existing frameworks/tools inevitably had
166 some sort of fundamental, fatal flaw that made us squeamish. No tool fit our
167 philosophies 100%.
168
169 Like we said: We're picky.
170
171 We've documented our philosophies on the `design philosophies page`_.
172
173 .. _design philosophies page: http://www.djangoproject.com/documentation/design_philosophies/
174
175 Do you have any of those nifty "screencast" things?
176 ---------------------------------------------------
177
178 You can bet your bottom they're on the way. But, since we're still hammering
179 out a few points, we want to make sure they reflect the final state of things
180 at Django 1.0, not some intermediary step. In other words, we don't want to
181 spend a lot of energy creating screencasts yet, because Django APIs will shift.
182
183 In the meantime, though, check out this `unofficial Django screencast`_.
184
185 .. _unofficial Django screencast: http://www.throwingbeans.org/django_screencasts.html
186
187 When will you release Django 1.0?
188 ---------------------------------
189
190 Short answer: When we're comfortable with Django's APIs, have added all
191 features that we feel are necessary to earn a "1.0" status, and are ready to
192 begin maintaining backwards compatibility. This should happen in a couple of
193 months or so, although it's entirely possible that it could happen earlier.
194 That translates into summer 2006.
195
196 The merging of Django's `magic-removal branch`_ went a long way toward Django
197 1.0.
198
199 Of course, you should note that `quite a few production sites`_ use Django in
200 its current status. Don't let the lack of a 1.0 turn you off.
201
202 .. _magic-removal branch: http://code.djangoproject.com/wiki/RemovingTheMagic
203 .. _quite a few production sites: http://code.djangoproject.com/wiki/DjangoPoweredSites
204
205 How can I download the Django documentation to read it offline?
206 ---------------------------------------------------------------
207
208 The Django docs are available in the ``docs`` directory of each Django tarball
209 release. These docs are in ReST (restructured text) format, and each text file
210 corresponds to a Web page on the official Django site.
211
212 Because the documentation is `stored in revision control`_, you can browse
213 documentation changes just like you can browse code changes.
214
215 Technically, the docs on Django's site are generated from the latest development
216 versions of those ReST documents, so the docs on the Django site may offer more
217 information than the docs that come with the latest Django release.
218
219 .. _stored in revision control: http://code.djangoproject.com/browser/django/trunk/docs
220
221 Installation questions
222 ======================
223
224 How do I get started?
225 ---------------------
226
227     #. `Download the code`_.
228     #. Install Django (read the `installation guide`_).
229     #. Walk through the tutorial_.
230     #. Check out the rest of the documentation_, and `ask questions`_ if you
231        run into trouble.
232
233 .. _`Download the code`: http://www.djangoproject.com/download/
234 .. _`installation guide`: http://www.djangoproject.com/documentation/install/
235 .. _tutorial:  http://www.djangoproject.com/documentation/tutorial1/
236 .. _documentation: http://www.djangoproject.com/documentation/
237 .. _ask questions: http://www.djangoproject.com/community/
238
239 How do I fix the "install a later version of setuptools" error?
240 ---------------------------------------------------------------
241
242 Just run the ``ez_setup.py`` script in the Django distribution.
243
244 What are Django's prerequisites?
245 --------------------------------
246
247 Django requires Python_ 2.3 or later. No other Python libraries are required.
248
249 For a development environment -- if you just want to experiment with Django --
250 you don't need to have a separate Web server installed; Django comes with its
251 own lightweight development server. For a production environment, we recommend
252 `Apache 2`_ and mod_python_, although Django follows the WSGI_ spec, which
253 means it can run on a variety of server platforms.
254
255 You'll also need a database engine. PostgreSQL_ is recommended, and MySQL_
256 and `SQLite 3`_ are supported.
257
258 .. _Python: http://www.python.org/
259 .. _Apache 2: http://httpd.apache.org/
260 .. _mod_python: http://www.modpython.org/
261 .. _WSGI: http://www.python.org/peps/pep-0333.html
262 .. _PostgreSQL: http://www.postgresql.org/
263 .. _MySQL: http://www.mysql.com/
264 .. _`SQLite 3`: http://www.sqlite.org/
265
266 Do I have to use mod_python?
267 ----------------------------
268
269 Not if you just want to play around and develop things on your local computer.
270 Django comes with its own Web server, and things should Just Work.
271
272 For production use, though, we recommend mod_python. The Django developers have
273 been running it on mod_python for more than two years, and it's quite stable.
274
275 However, if you don't want to use mod_python, you can use a different server,
276 as long as that server has WSGI_ hooks. See the `server arrangements wiki page`_.
277
278 .. _WSGI: http://www.python.org/peps/pep-0333.html
279 .. _server arrangements wiki page: http://code.djangoproject.com/wiki/ServerArrangements
280
281 How do I install mod_python on Windows?
282 ---------------------------------------
283
284     * For Python 2.4, check out this `guide to mod_python & Python 2.3`_.
285     * For Python 2.3, grab mod_python from http://www.modpython.org/ and read
286       `Running mod_python on Apache on Windows2000`_.
287     * Also, try this (not Windows-specific) `guide to getting mod_python
288       working`_.
289
290 .. _`guide to mod_python & Python 2.3`: http://www.lehuen.com/nicolas/index.php/2005/02/21/39-win32-build-of-mod_python-314-for-python-24
291 .. _`Running mod_python on Apache on Windows2000`: http://groups-beta.google.com/group/comp.lang.python/msg/139af8c83a5a9d4f
292 .. _`guide to getting mod_python working`: http://www.dscpl.com.au/articles/modpython-001.html
293
294 Will Django run under shared hosting (like TextDrive or Dreamhost)?
295 -------------------------------------------------------------------
296
297 See our `Django-friendly Web hosts`_ page.
298
299 .. _`Django-friendly Web hosts`: http://code.djangoproject.com/wiki/DjangoFriendlyWebHosts
300
301 Should I use the official version or development version?
302 ---------------------------------------------------------
303
304 The Django developers improve Django every day and are pretty good about not
305 checking in broken code. We use the development code (from the Subversion
306 repository) directly on our servers, so we consider it stable. With that in
307 mind, we recommend that you use the latest development code, because it
308 generally contains more features and fewer bugs than the "official" releases.
309
310 Using Django
311 ============
312
313 Why do I get an error about importing DJANGO_SETTINGS_MODULE?
314 -------------------------------------------------------------
315
316 Make sure that:
317
318     * The environment variable DJANGO_SETTINGS_MODULE is set to a fully-qualified
319       Python module (i.e. "mysite.settings.main").
320
321     * Said module is on ``sys.path`` (``import mysite.settings.main`` should work).
322
323     * The module doesn't contain syntax errors (of course).
324
325     * If you're using mod_python but *not* using Django's request handler,
326       you'll need to work around a mod_python bug related to the use of
327       ``SetEnv``; before you import anything from Django you'll need to do
328       the following::
329
330             os.environ.update(req.subprocess_env)
331
332       (where ``req`` is the mod_python request object).
333
334 I can't stand your template language. Do I have to use it?
335 ----------------------------------------------------------
336
337 We happen to think our template engine is the best thing since chunky bacon,
338 but we recognize that choosing a template language runs close to religion.
339 There's nothing about Django that requires using the template language, so
340 if you're attached to ZPT, Cheetah, or whatever, feel free to use those.
341
342 Do I have to use your model/database layer?
343 -------------------------------------------
344
345 Nope. Just like the template system, the model/database layer is decoupled from
346 the rest of the framework. The one exception is: If you use a different
347 database library, you won't get to use Django's automatically-generated admin
348 site. That app is coupled to the Django database layer.
349
350 How do I use image and file fields?
351 -----------------------------------
352
353 Using a ``FileField`` or an ``ImageField`` in a model takes a few steps:
354
355     #. In your settings file, define ``MEDIA_ROOT`` as the full path to
356        a directory where you'd like Django to store uploaded files. (For
357        performance, these files are not stored in the database.) Define
358        ``MEDIA_URL`` as the base public URL of that directory. Make sure that
359        this directory is writable by the Web server's user account.
360
361     #. Add the ``FileField`` or ``ImageField`` to your model, making sure
362        to define the ``upload_to`` option to tell Django to which subdirectory
363        of ``MEDIA_ROOT`` it should upload files.
364
365     #. All that will be stored in your database is a path to the file
366        (relative to ``MEDIA_ROOT``). You'll must likely want to use the
367        convenience ``get_<fieldname>_url`` function provided by Django. For
368        example, if your ``ImageField`` is called ``mug_shot``, you can get the
369        absolute URL to your image in a template with
370        ``{{ object.get_mug_shot_url }}``.
371
372 If I make changes to a model, how do I update the database?
373 -----------------------------------------------------------
374
375 If you don't mind clearing data, just pipe the output of the appropriate
376 ``django-admin.py sqlreset`` command into your database's command-line utility.
377 For example::
378
379     django-admin.py sqlreset appname | psql dbname
380
381 That "psql" assumes you're using PostgreSQL. If you're using MySQL, use the
382 appropriate command-line utility, ``mysql``.
383
384 ``django-admin.py sqlreset`` outputs SQL that clears the app's database
385 table(s) and creates new ones. The above command uses a Unix pipe to send the
386 SQL directly to the PostgreSQL command-line utility, which accepts SQL as
387 input.
388
389 If you do care about deleting data, you'll have to execute the ``ALTER TABLE``
390 statements manually in your database. That's the way we've always done it,
391 because dealing with data is a very sensitive operation that we've wanted to
392 avoid automating. That said, there's some work being done to add partially
393 automated database-upgrade functionality.
394
395 Do Django models support multiple-column primary keys?
396 ------------------------------------------------------
397
398 No. Only single-column primary keys are supported.
399
400 But this isn't an issue in practice, because there's nothing stopping you from
401 adding other constraints (using the ``unique_together`` model option or
402 creating the constraint directly in your database), and enforcing the
403 uniqueness at that level. Single-column primary keys are needed for things such
404 as the admin interface to work; e.g., you need a simple way of being able to
405 specify an object to edit or delete.
406
407 The database API
408 ================
409
410 How can I see the raw SQL queries Django is running?
411 ----------------------------------------------------
412
413 Make sure your Django ``DEBUG`` setting is set to ``True``. Then, just do
414 this::
415
416     >>> from django.db import connection
417     >>> connection.queries
418     [{'sql': 'SELECT polls_polls.id,polls_polls.question,polls_polls.pub_date FROM polls_polls',
419     'time': '0.002'}]
420
421 ``connection.queries`` is only available if ``DEBUG`` is ``True``. It's a list
422 of dictionaries in order of query execution. Each dictionary has the following::
423
424     ``sql`` -- The raw SQL statement
425     ``time`` -- How long the statement took to execute, in seconds.
426
427 ``connection.queries`` includes all SQL statements -- INSERTs, UPDATES,
428 SELECTs, etc. Each time your app hits the database, the query will be recorded.
429
430 Can I use Django with a pre-existing database?
431 ----------------------------------------------
432
433 Yes. See `Integrating with a legacy database`_.
434
435 .. _`Integrating with a legacy database`: http://www.djangoproject.com/documentation/legacy_databases/
436
437 The admin site
438 ==============
439
440 I can't log in. When I enter a valid username and password, it just brings up the login page again, with no error messages.
441 ---------------------------------------------------------------------------------------------------------------------------
442
443 The login cookie isn't being set correctly, because the domain of the cookie
444 sent out by Django doesn't match the domain in your browser. Try these two
445 things:
446
447     * Set the ``SESSION_COOKIE_DOMAIN`` setting in your admin config file
448       to match your domain. For example, if you're going to
449       "http://www.mysite.com/admin/" in your browser, in
450       "myproject.settings" you should set ``SESSION_COOKIE_DOMAIN = 'www.mysite.com'``.
451
452     * Some browsers (Firefox?) don't like to accept cookies from domains that
453       don't have dots in them. If you're running the admin site on "localhost"
454       or another domain that doesn't have a dot in it, try going to
455       "localhost.localdomain" or "127.0.0.1". And set
456       ``SESSION_COOKIE_DOMAIN`` accordingly.
457
458 I can't log in. When I enter a valid username and password, it brings up the login page again, with a "Please enter a correct username and password" error.
459 -----------------------------------------------------------------------------------------------------------------------------------------------------------
460
461 If you're sure your username and password are correct, make sure your user
462 account has ``is_active`` and ``is_staff`` set to True. The admin site only
463 allows access to users with those two fields both set to True.
464
465 How do I automatically set a field's value to the user who last edited the object in the admin?
466 -----------------------------------------------------------------------------------------------
467
468 At this point, you can't do this. But it's an oft-requested feature, so we're
469 discussing how it can be implemented. The problem is we don't want to couple
470 the model layer with the admin layer with the request layer (to get the current
471 user). It's a tricky problem.
472
473 How do I limit admin access so that objects can only be edited by the users who created them?
474 ---------------------------------------------------------------------------------------------
475
476 See the answer to the previous question.
477
478 My admin-site CSS and images showed up fine using the development server, but they're not displaying when using mod_python.
479 ---------------------------------------------------------------------------------------------------------------------------
480
481 See `serving the admin files`_ in the "How to use Django with mod_python"
482 documentation.
483
484 .. _serving the admin files: http://www.djangoproject.com/documentation/modpython/#serving-the-admin-files
485
486 My "list_filter" contains a ManyToManyField, but the filter doesn't display.
487 ----------------------------------------------------------------------------
488
489 Django won't bother displaying the filter for a ``ManyToManyField`` if there
490 are fewer than two related objects.
491
492 For example, if your ``list_filter`` includes ``sites``, and there's only one
493 site in your database, it won't display a "Site" filter. In that case,
494 filtering by site would be meaningless.
495
496 How can I customize the functionality of the admin interface?
497 -------------------------------------------------------------
498
499 You've got several options. If you want to piggyback on top of an add/change
500 form that Django automatically generates, you can attach arbitrary JavaScript
501 modules to the page via the model's ``class Admin`` ``js`` parameter. That
502 parameter is a list of URLs, as strings, pointing to JavaScript modules that
503 will be included within the admin form via a ``<script>`` tag.
504
505 If you want more flexibility than simply tweaking the auto-generated forms,
506 feel free to write custom views for the admin. The admin is powered by Django
507 itself, and you can write custom views that hook into the authentication
508 system, check permissions and do whatever else they need to do.
509
510 If you want to customize the look-and-feel of the admin interface, read the
511 next question.
512
513 The dynamically-generated admin site is ugly! How can I change it?
514 ------------------------------------------------------------------
515
516 We like it, but if you don't agree, you can modify the admin site's
517 presentation by editing the CSS stylesheet and/or associated image files. The
518 site is built using semantic HTML and plenty of CSS hooks, so any changes you'd
519 like to make should be possible by editing the stylesheet. We've got a
520 `guide to the CSS used in the admin`_ to get you started.
521
522 .. _`guide to the CSS used in the admin`: http://www.djangoproject.com/documentation/admin_css/
523
524 How do I create users without having to edit password hashes?
525 -------------------------------------------------------------
526
527 We don't recommend you create users via the admin interface, because at the
528 moment it requires you to edit password hashes manually. (Passwords are hashed
529 using one-way hash algorithms for security; there's currently no Web interface
530 for changing passwords by entering the actual password rather than the hash.)
531
532 To create a user, you'll have to use the Python API. See `creating users`_ for
533 full info.
534
535 .. _creating users: http://www.djangoproject.com/documentation/authentication/#creating-users
Note: See TracBrowser for help on using the browser.