Django

Code

root/django/branches/0.90-bugfixes/docs/faq.txt

Revision 1238, 17.7 kB (checked in by adrian, 3 years ago)

Followup faq change to [1237]

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 Using Django
244 ============
245
246 Why do I get an error about importing DJANGO_SETTINGS_MODULE?
247 -------------------------------------------------------------
248
249 Make sure that:
250
251     * The environment variable DJANGO_SETTINGS_MODULE is set to a fully-qualified
252       Python module (i.e. "mysite.settings.main").
253
254     * Said module is on ``sys.path`` (``import mysite.settings.main`` should work).
255
256     * The module doesn't contain syntax errors (of course).
257
258     * If you're using mod_python but *not* using Django's request handler,
259       you'll need to work around a mod_python bug related to the use of
260       ``SetEnv``; before you import anything from Django you'll need to do
261       the following::
262
263             os.environ.update(req.subprocess_env)
264
265       (where ``req`` is the mod_python request object).
266
267 I can't stand your template language. Do I have to use it?
268 ----------------------------------------------------------
269
270 We happen to think our template engine is the best thing since chunky bacon,
271 but we recognize that choosing a template language runs close to religion.
272 There's nothing about Django that requires using the template language, so
273 if you're attached to ZPT, Cheetah, or whatever, feel free to use those.
274
275 How do I use image and file fields?
276 -----------------------------------
277
278 Using a ``FileField`` or an ``ImageField`` in a model takes a few steps:
279
280     #. In your settings file, define ``MEDIA_ROOT`` as the full path to
281        a directory where you'd like Django to store uploaded files. (For
282        performance, these files are not stored in the database.) Define
283        ``MEDIA_URL`` as the base public URL of that directory. Make sure that
284        this directory is writable by the Web server's user account.
285
286     #. Add the ``FileField`` or ``ImageField`` to your model, making sure
287        to define the ``upload_to`` option to tell Django to which subdirectory
288        of ``MEDIA_ROOT`` it should upload files.
289
290     #. All that will be stored in your database is a path to the file
291        (relative to ``MEDIA_ROOT``). You'll must likely want to use the
292        convenience ``get_<fieldname>_url`` function provided by Django. For
293        example, if your ``ImageField`` is called ``mug_shot``, you can get the
294        absolute URL to your image in a template with
295        ``{{ object.get_mug_shot_url }}``.
296
297 If I make changes to a model, how do I update the database?
298 -----------------------------------------------------------
299
300 If you don't care about clearing data, just do this::
301
302     django-admin.py sqlreset appname | psql dbname
303
304 That "psql" assumes you're using PostgreSQL. If you're using MySQL, use the
305 appropriate command-line utility, ``mysql``.
306
307 ``django-admin.py sqlreset`` outputs SQL that clears the app's database
308 table(s) and creates new ones. The above command uses a Unix pipe to send the
309 SQL directly to the PostgreSQL command-line utility, which accepts SQL as
310 input.
311
312 If you do care about deleting data, you'll have to execute the ``ALTER TABLE``
313 statements manually in your database. That's the way we've always done it,
314 because dealing with data is a very sensitive operation that we've wanted to
315 avoid automating. That said, there's some work being done to add a
316 ``django-admin.py updatedb`` command, which would output the necessary
317 ``ALTER TABLE`` statements, if any.
318
319 The database API
320 ================
321
322 How can I see the raw SQL queries Django is running?
323 ----------------------------------------------------
324
325 Make sure your Django ``DEBUG`` setting is set to ``True``. Then, just do
326 this::
327
328     >>> from django.core.db import db
329     >>> db.queries
330     [{'sql': 'SELECT polls_polls.id,polls_polls.question,polls_polls.pub_date FROM polls_polls',
331     'time': '0.002'}]
332
333 ``db.queries`` is only available if ``DEBUG`` is ``True``. It's a list of
334 dictionaries in order of query execution. Each dictionary has the following::
335
336     ``sql`` -- The raw SQL statement
337     ``time`` -- How long the statement took to execute, in seconds.
338
339 ``db.queries`` includes all SQL statements -- INSERTs, UPDATES, SELECTs, etc.
340
341 Can I use Django with a pre-existing database?
342 ----------------------------------------------
343
344 Yes. See `Integrating with a legacy database`_.
345
346 .. _`Integrating with a legacy database`: http://www.djangoproject.com/documentation/legacy_databases/
347
348 The admin site
349 ==============
350
351 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.
352 ---------------------------------------------------------------------------------------------------------------------------
353
354 The login cookie isn't being set correctly, because the domain of the cookie
355 sent out by Django doesn't match the domain in your browser. Try these two
356 things:
357
358     * Set the ``SESSION_COOKIE_DOMAIN`` setting in your admin config file
359       to match your domain. For example, if you're going to
360       "http://www.mysite.com/admin/" in your browser, in
361       "myproject.settings" you should set ``SESSION_COOKIE_DOMAIN = 'www.mysite.com'``.
362
363     * Some browsers (Firefox?) don't like to accept cookies from domains that
364       don't have dots in them. If you're running the admin site on "localhost"
365       or another domain that doesn't have a dot in it, try going to
366       "localhost.localdomain" or "127.0.0.1". And set
367       ``SESSION_COOKIE_DOMAIN`` accordingly.
368
369 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.
370 -----------------------------------------------------------------------------------------------------------------------------------------------------------
371
372 If you're sure your username and password are correct, make sure your user
373 account has ``is_active`` and ``is_staff`` set to True. The admin site only
374 allows access to users with those two fields both set to True.
375
376 My "list_filter" contains a ManyToManyField, but the filter doesn't display.
377 ----------------------------------------------------------------------------
378
379 Django won't bother displaying the filter for a ManyToManyField if there are
380 fewer than two related objects.
381
382 For example, if your ``list_filter`` includes ``sites``, and there's only one
383 site in your database, it won't display a "Site" filter. In that case,
384 filtering by site would be meaningless.
385
386 How can I customize the functionality of the admin interface?
387 -------------------------------------------------------------
388
389 You've got several options. If you want to piggyback on top of an add/change
390 form that Django automatically generates, you can attach arbitrary JavaScript
391 modules to the page via the model's ``admin.js`` parameter. That parameter is
392 a list of URLs, as strings, pointing to JavaScript modules that will be
393 included within the admin form via a <script> tag.
394
395 If you want more flexibility than simply tweaking the auto-generated forms,
396 feel free to write custom views for the admin. The admin is powered by Django
397 itself, and you can write custom views that hook into the authentication
398 system, check permissions and do whatever else they need to do.
399
400 If you want to customize the look-and-feel of the admin interface, read the
401 next question.
402
403 The dynamically-generated admin site is ugly! How can I change it?
404 ------------------------------------------------------------------
405
406 We think it's very purty, but if you don't agree, you can modify the admin
407 site's presentation by editing the CSS stylesheet and/or associated image files.
408 The site is built using semantic HTML, so any changes you'd like to make should
409 be possible by editing the CSS stylesheet. We've got a `guide to the CSS used in
410 the admin`_ to get you started.
411
412 .. _`guide to the CSS used in the admin`: http://www.djangoproject.com/documentation/admin_css/
Note: See TracBrowser for help on using the browser.