Django

Code

root/django/branches/new-admin/docs/faq.txt

Revision 1321, 18.1 kB (checked in by rjwittams, 3 years ago)

Merged to trunk r1320

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: in our fast-paced newsroom, we often
12 have only a matter of hours to take a complicated Web application from
13 concept to public launch.  Django was designed to not only allow us to
14 build Web applications quickly, but to allow us to build them right.
15
16 Django would not be possible without a whole host of open-source projects --
17 `Apache`_, `Python`_, and `PostgreSQL`_ to name a few -- and we're thrilled to be
18 able to give something back to the open-source community.
19
20 .. _Apache: http://httpd.apache.org/
21 .. _Python: http://www.python.org/
22 .. _PostgreSQL: http://www.postgresql.org/
23
24 What does "Django" mean, and how do you pronounce it?
25 -----------------------------------------------------
26
27 Django is named after `Django Reinhardt`_, a gypsy jazz guitarist from the 1930s
28 to early 1950s. To this day, he's considered one of the best guitarists of all time.
29
30 Listen to his music. You'll like it.
31
32 According to Wikipedia_, "Django is pronounced **zhane**-go (with a long 'a')."
33
34 .. _Django Reinhardt: http://en.wikipedia.org/wiki/Django_Reinhardt
35 .. _Wikipedia: http://en.wikipedia.org/wiki/Django_Reinhardt
36
37 Is Django stable?
38 -----------------
39
40 We've been using Django for almost two years. Sites built on Django have
41 weathered traffic spikes of over one million hits an hour, and at least
42 one Slashdotting. Yes, it's quite stable.
43
44 Does Django scale?
45 ------------------
46
47 Yes. Compared to development time, hardware is cheap, and so Django is
48 designed to take advantage of as much hardware as you can throw at it.
49 Django ships with clean separation of the database layer from the
50 application layer and a simple-yet-powerful `cache framework`_.
51
52 .. _`cache framework`: http://www.djangoproject.com/documentation/cache/
53
54 Who's behind this?
55 ------------------
56
57 Django was developed at `World Online`_, the Web department of a newspaper in
58 Lawrence, Kansas, USA.
59
60 `Adrian Holovaty`_
61     Adrian is a Web developer with a background in journalism. He was lead
62     developer at World Online for 2.5 years, during which time Django was
63     developed and implemented on World Online's sites. Now he's editor of
64     editorial innovations at washingtonpost.com, and he continues to oversee
65     Django development. He likes playing guitar (Django Reinhardt style) and
66     hacking on side projects such as `chicagocrime.org`_. He lives in Chicago.
67
68     On IRC, Adrian goes by ``adrian_h``.
69
70 `Simon Willison`_
71     Simon is a well-respected Web developer from England. He had a one-year
72     internship at World Online, during which time he and Adrian developed
73     Django from scratch. He's enthusiastic, he's passionate about best
74     practices in Web development, and he really likes squirrels. Probably to a
75     fault. He went back to university to finish his degree and is poised to
76     continue doing big, exciting things on the Web. He lives in England.
77
78     On IRC, Simon goes by ``SimonW``.
79
80 `Jacob Kaplan-Moss`_
81     Jacob is a whipper-snapper from California who spends equal time coding and
82     cooking. He does Web development for World Online and actively hacks on
83     various cool side projects. He's contributed to the Python-ObjC bindings and
84     was the first guy to figure out how to write Tivo apps in Python. Lately
85     he's been messing with Python on the PSP. He lives in Lawrence, Kansas.
86
87     On IRC, Jacob goes by ``jacobkm``.
88
89 `Wilson Miner`_
90     Wilson's design-fu makes us all look like rock stars. When not sneaking
91     into apartment complex swimming pools, he's the Commercial Development
92     Director for World Online, which means he makes the money that pays all our
93     paychecks. He lives in Lawrence, Kansas.
94
95     On IRC, Wilson goes by ``wilsonian``.
96
97 .. _`World Online`: http://code.djangoproject.com/wiki/WorldOnline
98 .. _`Adrian Holovaty`: http://www.holovaty.com/
99 .. _`chicagocrime.org`: http://www.chicagocrime.org/
100 .. _`Simon Willison`: http://simon.incutio.com/
101 .. _`simon.incutio.com`: http://simon.incutio.com/
102 .. _`Jacob Kaplan-Moss`: http://www.jacobian.org/
103 .. _`Wilson Miner`: http://www.wilsonminer.com/live/
104
105 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?
106 -----------------------------------------------------------------------------------------------------------------------------------------------------
107
108 That's because Django isn't strictly a MVC framework. We don't really believe in
109 any capital-M Methodologies; we do what "feels" right. If you squint the right
110 way, you can call Django's ORM the "Model", the view functions the "View", and
111 the dynamically-generated API the "Controller" -- but not really.
112
113 In fact, you might say that Django is a "MTV" framework -- that is, Model,
114 Template, and View make much more sense to us.
115
116 So, although we've been strongly influenced by MVC -- especially in the
117 separation-of-data-from-logic department -- we've also strayed from the path
118 where it makes sense.
119
120 <Framework X> does <feature Y> -- why doesn't Django?
121 -----------------------------------------------------
122
123 We're well aware that there are other awesome Web frameworks out there, and
124 we're not adverse to borrowing ideas where appropriate. However, Django was
125 developed precisely because we were unhappy with the status quo, so please be
126 aware that "because <Framework X>" does it is not going to be sufficient reason
127 to add a given feature to Django.
128
129 Why did you write all of Django from scratch, instead of using other Python libraries?
130 --------------------------------------------------------------------------------------
131
132 When Django was originally written a couple of years ago, Adrian and Simon
133 spent quite a bit of time exploring the various Python Web frameworks
134 available.
135
136 In our opinion, none of them were completely up to snuff.
137
138 We're picky. You might even call us perfectionists. (With deadlines.)
139
140 Over time, we stumbled across open-source libraries that did things we'd
141 already implemented. It was reassuring to see other people solving similar
142 problems in similar ways, but it was too late to integrate outside code: We'd
143 already written, tested and implemented our own framework bits in several
144 production settings -- and our own code met our needs delightfully.
145
146 In most cases, however, we found that existing frameworks/tools inevitably had
147 some sort of fundamental, fatal flaw that made us squeamish. No tool fit our
148 philosophies 100%.
149
150 Like we said: We're picky.
151
152 We've documented our philosophies on the `design philosophies page`_.
153
154 .. _design philosophies page: http://www.djangoproject.com/documentation/design_philosophies/
155
156 Do you have any of those nifty "screencast" things?
157 ---------------------------------------------------
158
159 They're in the works. It's amazing how much time those things take! Stay tuned...
160
161 Installation questions
162 ======================
163
164 How do I get started?
165 ---------------------
166
167     #. `Download the code`_.
168     #. Install Django (read the `installation guide`_).
169     #. Walk through the tutorial_.
170     #. Check out the rest of the documentation_, and `ask questions`_ if you
171        run into trouble.
172
173 .. _`Download the code`: http://www.djangoproject.com/download/
174 .. _`installation guide`: http://www.djangoproject.com/documentation/install/
175 .. _tutorial:  http://www.djangoproject.com/documentation/tutorial1/
176 .. _documentation: http://www.djangoproject.com/documentation/
177 .. _ask questions: http://www.djangoproject.com/community/
178
179 How do I fix the "install a later version of setuptools" error?
180 ---------------------------------------------------------------
181
182 Just run the ``ex_setup.py`` script in the Django distribution.
183
184 What are Django's prerequisites?
185 --------------------------------
186
187 Django requires Python_ 2.3 or later.
188
189 For a development environment -- if you just want to experiment with Django --
190 you don't need to have a separate Web server installed; Django comes with its
191 own lightweight development server. For a production environment, we recommend
192 `Apache 2`_ and mod_python_, although Django follows the WSGI_ spec, which
193 means it can run on a variety of server platforms.
194
195 You'll also need a database engine. PostgreSQL_ is recommended, and MySQL_
196 and `SQLite 3`_ are supported.
197
198 .. _Python: http://www.python.org/
199 .. _Apache 2: http://httpd.apache.org/
200 .. _mod_python: http://www.modpython.org/
201 .. _WSGI: http://www.python.org/peps/pep-0333.html
202 .. _PostgreSQL: http://www.postgresql.org/
203 .. _MySQL: http://www.mysql.com/
204 .. _`SQLite 3`: http://www.sqlite.org/
205
206 Do I have to use mod_python?
207 ----------------------------
208
209 Not if you just want to play around and develop things on your local computer.
210 Django comes with its own Web server, and things should Just Work.
211
212 For production use, though, we recommend mod_python. The Django developers have
213 been running it on mod_python for about two years, and it's quite stable.
214
215 However, if you don't want to use mod_python, you can use a different server,
216 as long as that server has WSGI_ hooks. More information on alternate server
217 arrangements is forthcoming.
218
219 .. _WSGI: http://www.python.org/peps/pep-0333.html
220
221 How do I install mod_python on Windows?
222 ---------------------------------------
223
224     * For Python 2.4, check out this `guide to mod_python & Python 2.3`_.
225     * For Python 2.3, grab mod_python from http://www.modpython.org/ and read
226       `Running mod_python on Apache on Windows2000`_.
227     * Also, try this (not Windows-specific) `guide to getting mod_python
228       working`_.
229
230 .. _`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
231 .. _`Running mod_python on Apache on Windows2000`: http://groups-beta.google.com/group/comp.lang.python/msg/139af8c83a5a9d4f
232 .. _`guide to getting mod_python working`: http://www.dscpl.com.au/articles/modpython-001.html
233
234 (Thanks to deelan for this info.)
235
236 Will Django run under shared hosting (like TextDrive or Dreamhost)?
237 -------------------------------------------------------------------
238
239 See our `Django-friendly Web hosts`_ page.
240
241 .. _`Django-friendly Web hosts`: http://code.djangoproject.com/wiki/DjangoFriendlyWebHosts
242
243 Should I use the official version or development version?
244 ---------------------------------------------------------
245
246 The Django developers improve Django every day and are pretty good about not
247 checking in broken code. We use the development code (from the Subversion
248 repository) directly on our servers, so we consider it stable. With that in
249 mind, we recommend that you use the latest development code, because it
250 generally contains more features and fewer bugs than the "official" releases.
251
252 Using Django
253 ============
254
255 Why do I get an error about importing DJANGO_SETTINGS_MODULE?
256 -------------------------------------------------------------
257
258 Make sure that:
259
260     * The environment variable DJANGO_SETTINGS_MODULE is set to a fully-qualified
261       Python module (i.e. "mysite.settings.main").
262
263     * Said module is on ``sys.path`` (``import mysite.settings.main`` should work).
264
265     * The module doesn't contain syntax errors (of course).
266
267     * If you're using mod_python but *not* using Django's request handler,
268       you'll need to work around a mod_python bug related to the use of
269       ``SetEnv``; before you import anything from Django you'll need to do
270       the following::
271
272             os.environ.update(req.subprocess_env)
273
274       (where ``req`` is the mod_python request object).
275
276 I can't stand your template language. Do I have to use it?
277 ----------------------------------------------------------
278
279 We happen to think our template engine is the best thing since chunky bacon,
280 but we recognize that choosing a template language runs close to religion.
281 There's nothing about Django that requires using the template language, so
282 if you're attached to ZPT, Cheetah, or whatever, feel free to use those.
283
284 How do I use image and file fields?
285 -----------------------------------
286
287 Using a ``FileField`` or an ``ImageField`` in a model takes a few steps:
288
289     #. In your settings file, define ``MEDIA_ROOT`` as the full path to
290        a directory where you'd like Django to store uploaded files. (For
291        performance, these files are not stored in the database.) Define
292        ``MEDIA_URL`` as the base public URL of that directory. Make sure that
293        this directory is writable by the Web server's user account.
294
295     #. Add the ``FileField`` or ``ImageField`` to your model, making sure
296        to define the ``upload_to`` option to tell Django to which subdirectory
297        of ``MEDIA_ROOT`` it should upload files.
298
299     #. All that will be stored in your database is a path to the file
300        (relative to ``MEDIA_ROOT``). You'll must likely want to use the
301        convenience ``get_<fieldname>_url`` function provided by Django. For
302        example, if your ``ImageField`` is called ``mug_shot``, you can get the
303        absolute URL to your image in a template with
304        ``{{ object.get_mug_shot_url }}``.
305
306 If I make changes to a model, how do I update the database?
307 -----------------------------------------------------------
308
309 If you don't care about clearing data, just do this::
310
311     django-admin.py sqlreset appname | psql dbname
312
313 That "psql" assumes you're using PostgreSQL. If you're using MySQL, use the
314 appropriate command-line utility, ``mysql``.
315
316 ``django-admin.py sqlreset`` outputs SQL that clears the app's database
317 table(s) and creates new ones. The above command uses a Unix pipe to send the
318 SQL directly to the PostgreSQL command-line utility, which accepts SQL as
319 input.
320
321 If you do care about deleting data, you'll have to execute the ``ALTER TABLE``
322 statements manually in your database. That's the way we've always done it,
323 because dealing with data is a very sensitive operation that we've wanted to
324 avoid automating. That said, there's some work being done to add a
325 ``django-admin.py updatedb`` command, which would output the necessary
326 ``ALTER TABLE`` statements, if any.
327
328 The database API
329 ================
330
331 How can I see the raw SQL queries Django is running?
332 ----------------------------------------------------
333
334 Make sure your Django ``DEBUG`` setting is set to ``True``. Then, just do
335 this::
336
337     >>> from django.core.db import db
338     >>> db.queries
339     [{'sql': 'SELECT polls_polls.id,polls_polls.question,polls_polls.pub_date FROM polls_polls',
340     'time': '0.002'}]
341
342 ``db.queries`` is only available if ``DEBUG`` is ``True``. It's a list of
343 dictionaries in order of query execution. Each dictionary has the following::
344
345     ``sql`` -- The raw SQL statement
346     ``time`` -- How long the statement took to execute, in seconds.
347
348 ``db.queries`` includes all SQL statements -- INSERTs, UPDATES, SELECTs, etc.
349
350 Can I use Django with a pre-existing database?
351 ----------------------------------------------
352
353 Yes. See `Integrating with a legacy database`_.
354
355 .. _`Integrating with a legacy database`: http://www.djangoproject.com/documentation/legacy_databases/
356
357 The admin site
358 ==============
359
360 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.
361 ---------------------------------------------------------------------------------------------------------------------------
362
363 The login cookie isn't being set correctly, because the domain of the cookie
364 sent out by Django doesn't match the domain in your browser. Try these two
365 things:
366
367     * Set the ``SESSION_COOKIE_DOMAIN`` setting in your admin config file
368       to match your domain. For example, if you're going to
369       "http://www.mysite.com/admin/" in your browser, in
370       "myproject.settings" you should set ``SESSION_COOKIE_DOMAIN = 'www.mysite.com'``.
371
372     * Some browsers (Firefox?) don't like to accept cookies from domains that
373       don't have dots in them. If you're running the admin site on "localhost"
374       or another domain that doesn't have a dot in it, try going to
375       "localhost.localdomain" or "127.0.0.1". And set
376       ``SESSION_COOKIE_DOMAIN`` accordingly.
377
378 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.
379 -----------------------------------------------------------------------------------------------------------------------------------------------------------
380
381 If you're sure your username and password are correct, make sure your user
382 account has ``is_active`` and ``is_staff`` set to True. The admin site only
383 allows access to users with those two fields both set to True.
384
385 My "list_filter" contains a ManyToManyField, but the filter doesn't display.
386 ----------------------------------------------------------------------------
387
388 Django won't bother displaying the filter for a ManyToManyField if there are
389 fewer than two related objects.
390
391 For example, if your ``list_filter`` includes ``sites``, and there's only one
392 site in your database, it won't display a "Site" filter. In that case,
393 filtering by site would be meaningless.
394
395 How can I customize the functionality of the admin interface?
396 -------------------------------------------------------------
397
398 You've got several options. If you want to piggyback on top of an add/change
399 form that Django automatically generates, you can attach arbitrary JavaScript
400 modules to the page via the model's ``admin.js`` parameter. That parameter is
401 a list of URLs, as strings, pointing to JavaScript modules that will be
402 included within the admin form via a <script> tag.
403
404 If you want more flexibility than simply tweaking the auto-generated forms,
405 feel free to write custom views for the admin. The admin is powered by Django
406 itself, and you can write custom views that hook into the authentication
407 system, check permissions and do whatever else they need to do.
408
409 If you want to customize the look-and-feel of the admin interface, read the
410 next question.
411
412 The dynamically-generated admin site is ugly! How can I change it?
413 ------------------------------------------------------------------
414
415 We think it's very purty, but if you don't agree, you can modify the admin
416 site's presentation by editing the CSS stylesheet and/or associated image files.
417 The site is built using semantic HTML, so any changes you'd like to make should
418 be possible by editing the CSS stylesheet. We've got a `guide to the CSS used in
419 the admin`_ to get you started.
420
421 .. _`guide to the CSS used in the admin`: http://www.djangoproject.com/documentation/admin_css/
Note: See TracBrowser for help on using the browser.