#

## Django documentation

# Django FAQ

## General questions

### Why does this project exist?

Django grew from a very practical need: in our fast-paced newsroom, we often have only a matter of hours to take a complicated Web application from concept to public launch. Django was designed to not only allow us to build Web applications quickly, but to allow us to build them right.

Django would not be possible without a whole host of open-source projects -- Apache, Python, and PostgreSQL to name a few -- and we're thrilled to be able to give something back to the open-source community.

### What does "Django" mean, and how do you pronounce it?

Django is named after Django Reinhardt, a gypsy jazz guitarist from the 1930s to early 1950s. To this day, he's considered one of the best guitarists of all time.

Listen to his music. You'll like it.

According to Wikipedia, "Django is pronounced zhane-go (with a long 'a')."

### Is Django stable?

We've been using Django for almost two years. Sites built on Django have weathered traffic spikes of over one million hits an hour, and at least one Slashdotting. Yes, it's quite stable.

### Does Django scale?

Yes. Compared to development time, hardware is cheap, and so Django is designed to take advantage of as much hardware as you can throw at it. Django ships with clean separation of the database layer from the application layer and a simple-yet-powerful cache framework.

### Who's behind this?

Django was developed at World Online, the Web department of a newspaper in Lawrence, Kansas, USA.

Adrian Holovaty

  Adrian is a Web developer with a background in journalism. He was lead developer at World Online for 2.5 years, during which time Django was developed and implemented on World Online's sites. Now he's editor of editorial innovations at washingtonpost.com, and he continues to oversee Django development. He likes playing guitar (Django Reinhardt style) and hacking on side projects such as chicagocrime.org. He lives in Chicago.

  On IRC, Adrian goes by `adrian_h`.

Simon Willison

  Simon is a well-respected Web developer from England. He had a one-year internship at World Online, during which time he and Adrian developed Django from scratch. He's enthusiastic, he's passionate about best practices in Web development, and he really likes squirrels. Probably to a fault. He went back to university to finish his degree and is poised to continue doing big, exciting things on the Web. He lives in England.

  On IRC, Simon goes by `SimonW`.

Jacob Kaplan-Moss

  Jacob is a whipper-snapper from California who spends equal time coding and cooking. He does Web development for World Online and actively hacks on various cool side projects. He's contributed to the Python-ObjC bindings and was the first guy to figure out how to write Tivo apps in Python. Lately he's been messing with Python on the PSP. He lives in Lawrence, Kansas.

  On IRC, Jacob goes by `jacobkm`.

Wilson Miner

  Wilson's design-fu makes us all look like rock stars. When not sneaking into apartment complex swimming pools, he's the Commercial Development Director for World Online, which means he makes the money that pays all our paychecks. He lives in Lawrence, Kansas.

  On IRC, Wilson goes by `wilsonian`.

### 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?

That's because Django isn't strictly a MVC framework. We don't really believe in any capital-M Methodologies; we do what "feels" right. If you squint the right way, you can call Django's ORM the "Model", the view functions the "View", and the dynamically-generated API the "Controller" -- but not really.

In fact, you might say that Django is a "MTV" framework -- that is, Model, Template, and View make much more sense to us.

So, although we've been strongly influenced by MVC -- especially in the separation-of-data-from-logic department -- we've also strayed from the path where it makes sense.

### <Framework X> does <feature Y> -- why doesn't Django?

We're well aware that there are other awesome Web frameworks out there, and we're not adverse to borrowing ideas where appropriate. However, Django was developed precisely because we were unhappy with the status quo, so please be aware that "because <Framework X>" does it is not going to be sufficient reason to add a given feature to Django.

### Why did you write all of Django from scratch, instead of using other Python libraries?

When Django was originally written a couple of years ago, Adrian and Simon spent quite a bit of time exploring the various Python Web frameworks available.

In our opinion, none of them were completely up to snuff.

We're picky. You might even call us perfectionists. (With deadlines.)

Over time, we stumbled across open-source libraries that did things we'd already implemented. It was reassuring to see other people solving similar problems in similar ways, but it was too late to integrate outside code: We'd already written, tested and implemented our own framework bits in several production settings -- and our own code met our needs delightfully.

In most cases, however, we found that existing frameworks/tools inevitably had some sort of fundamental, fatal flaw that made us squeamish. No tool fit our philosophies 100%.

Like we said: We're picky.

We've documented our philosophies on the design philosophies page.

### Do you have any of those nifty "screencast" things?

They're in the works. It's amazing how much time those things take! Stay tuned...

## Installation questions

### How do I get started?

> 1. Download the code.
> 2. Install Django (read the installation guide).
> 3. Walk through the tutorial.
> 4. Check out the rest of the documentation, and ask questions if you run into trouble.

### How do I fix the "install a later version of setuptools" error?

Just run the `ex_setup.py` script in the Django distribution.

### What are Django's prerequisites?

Django requires Python 2.3 or later.

For a development environment -- if you just want to experiment with Django -- you don't need to have a separate Web server installed; Django comes with its own lightweight development server. For a production environment, we recommend Apache 2 and mod_python, although Django follows the WSGI spec, which means it can run on a variety of server platforms.

You'll also need a database engine. PostgreSQL is recommended, and MySQL and SQLite 3 are supported.

### Do I have to use mod_python?

Not if you just want to play around and develop things on your local computer. Django comes with its own Web server, and things should Just Work.

For production use, though, we recommend mod_python. The Django developers have been running it on mod_python for about two years, and it's quite stable.

However, if you don't want to use mod_python, you can use a different server, as long as that server has WSGI hooks. More information on alternate server arrangements is forthcoming.

### How do I install mod_python on Windows?

> - For Python 2.4, check out this guide to mod_python & Python 2.3.
> - For Python 2.3, grab mod_python from http://www.modpython.org/ and read Running mod_python on Apache on Windows2000.
> - Also, try this (not Windows-specific) guide to getting mod_python working.

(Thanks to deelan for this info.)

### Will Django run under shared hosting (like TextDrive or Dreamhost)?

See our Django-friendly Web hosts page.

### Should I use the official version or development version?

The Django developers improve Django every day and are pretty good about not checking in broken code. We use the development code (from the Subversion repository) directly on our servers, so we consider it stable. With that in mind, we recommend that you use the latest development code, because it generally contains more features and fewer bugs than the "official" releases.

## Using Django

### Why do I get an error about importing DJANGO_SETTINGS_MODULE?

Make sure that:

> - The environment variable DJANGO_SETTINGS_MODULE is set to a fully-qualified Python module (i.e. "mysite.settings.main").

> - Said module is on `sys.path` (`import mysite.settings.main` should work).

> - The module doesn't contain syntax errors (of course).

> - If you're using mod_python but not using Django's request handler, you'll need to work around a mod_python bug related to the use of `SetEnv`; before you import anything from Django you'll need to do the following:

>       os.environ.update(req.subprocess_env)

>   (where `req` is the mod_python request object).

### I can't stand your template language. Do I have to use it?

We happen to think our template engine is the best thing since chunky bacon, but we recognize that choosing a template language runs close to religion. There's nothing about Django that requires using the template language, so if you're attached to ZPT, Cheetah, or whatever, feel free to use those.

### How do I use image and file fields?

Using a `FileField` or an `ImageField` in a model takes a few steps:

> 1. In your settings file, define `MEDIA_ROOT` as the full path to a directory where you'd like Django to store uploaded files. (For performance, these files are not stored in the database.) Define `MEDIA_URL` as the base public URL of that directory. Make sure that this directory is writable by the Web server's user account.
> 2. Add the `FileField` or `ImageField` to your model, making sure to define the `upload_to` option to tell Django to which subdirectory of `MEDIA_ROOT` it should upload files.
> 3. All that will be stored in your database is a path to the file (relative to `MEDIA_ROOT`). You'll must likely want to use the convenience `get_<fieldname>_url` function provided by Django. For example, if your `ImageField` is called `mug_shot`, you can get the absolute URL to your image in a template with `{{ object.get_mug_shot_url }}`.

### If I make changes to a model, how do I update the database?

If you don't care about clearing data, just do this:

    django-admin.py sqlreset appname | psql dbname

That "psql" assumes you're using PostgreSQL. If you're using MySQL, use the appropriate command-line utility, `mysql`.

`django-admin.py sqlreset` outputs SQL that clears the app's database table(s) and creates new ones. The above command uses a Unix pipe to send the SQL directly to the PostgreSQL command-line utility, which accepts SQL as input.

If you do care about deleting data, you'll have to execute the `ALTER TABLE` statements manually in your database. That's the way we've always done it, because dealing with data is a very sensitive operation that we've wanted to avoid automating. That said, there's some work being done to add a `django-admin.py updatedb` command, which would output the necessary `ALTER TABLE` statements, if any.

## The database API

### How can I see the raw SQL queries Django is running?

Make sure your Django `DEBUG` setting is set to `True`. Then, just do this:

    >>> from django.core.db import db
    >>> db.queries
    [{'sql': 'SELECT polls_polls.id,polls_polls.question,polls_polls.pub_date FROM polls_polls',
    'time': '0.002'}]

`db.queries` is only available if `DEBUG` is `True`. It's a list of dictionaries in order of query execution. Each dictionary has the following:

    ``sql`` -- The raw SQL statement
    ``time`` -- How long the statement took to execute, in seconds.

`db.queries` includes all SQL statements -- INSERTs, UPDATES, SELECTs, etc.

### Can I use Django with a pre-existing database?

Yes. See Integrating with a legacy database.

## The admin site

### 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.

The login cookie isn't being set correctly, because the domain of the cookie sent out by Django doesn't match the domain in your browser. Try these two things:

> - Set the `SESSION_COOKIE_DOMAIN` setting in your admin config file to match your domain. For example, if you're going to "http://www.mysite.com/admin/" in your browser, in "myproject.settings" you should set `SESSION_COOKIE_DOMAIN = 'www.mysite.com'`.
> - Some browsers (Firefox?) don't like to accept cookies from domains that don't have dots in them. If you're running the admin site on "localhost" or another domain that doesn't have a dot in it, try going to "localhost.localdomain" or "127.0.0.1". And set `SESSION_COOKIE_DOMAIN` accordingly.

### 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.

If you're sure your username and password are correct, make sure your user account has `is_active` and `is_staff` set to True. The admin site only allows access to users with those two fields both set to True.

### My "list_filter" contains a ManyToManyField, but the filter doesn't display.

Django won't bother displaying the filter for a ManyToManyField if there are fewer than two related objects.

For example, if your `list_filter` includes `sites`, and there's only one site in your database, it won't display a "Site" filter. In that case, filtering by site would be meaningless.

### How can I customize the functionality of the admin interface?

You've got several options. If you want to piggyback on top of an add/change form that Django automatically generates, you can attach arbitrary JavaScript modules to the page via the model's `admin.js` parameter. That parameter is a list of URLs, as strings, pointing to JavaScript modules that will be included within the admin form via a <script> tag.

If you want more flexibility than simply tweaking the auto-generated forms, feel free to write custom views for the admin. The admin is powered by Django itself, and you can write custom views that hook into the authentication system, check permissions and do whatever else they need to do.

If you want to customize the look-and-feel of the admin interface, read the next question.

### The dynamically-generated admin site is ugly! How can I change it?

We think it's very purty, but if you don't agree, you can modify the admin site's presentation by editing the CSS stylesheet and/or associated image files. The site is built using semantic HTML, so any changes you'd like to make should be possible by editing the CSS stylesheet. We've got a guide to the CSS used in the admin to get you started.

## Comments

### Lalo Martins July 21, 2005 at 11:01 p.m.

Q: what's with the verbose way to define fields? Why not use more meta-programming, like SQLObject (and therefore Subway) does?

(asked at http://griddlenoise.blogspot.com/2005/07... and other places)

A: defining the fields in a tuple preserves their order. It's a common annoyance many people have with SQLObject, that it's impossible to keep your columns in the right order. (That *could* be solved with some very wicked coding, but it's probably too much hassle for small benefit.)

Field order is very important, specially when you're auto-generating the admin interface. Some other frameworks, like Plone's Archetypes (http://plone.org/products/archetypes), also use tuples or lists to define schemas, for that precise reason.

(I'm not one of the Django authors, so I can't say for sure this is the only reason, but I can bet my lunch it's one of the reasons.)

### Adam Sherman August 17, 2005 at 5:53 p.m.

How can I update the database after a change to a model? Since I don't, yet, know exactly what a given model will generate for SQL, I can't really do it manually.

### Zane Love August 18, 2005 at 2:32 p.m.

Where/how do I set a long-running process going in Django? For instance, if I want to set up some kind of task-scheduler?

### Brendan O'Connor August 21, 2005 at 1:29 a.m.

re the metaprogrammed field definition: I understand that the blog post you referenced is saying

this is clunky:

fields = (meta.CharField('email', length=50), ...)

this is nicer:

email = meta.CharField(length=50)

I agree, but diagree that order has anything to do with it. For declarations, that's a fairly minor change. Just have to do the metaclass hackery that SQLObject and the Chandler Repository both do.

There's some issues implementing this inside Django though. sqlobject-like syntax has the disadvantage of using the class attribute namespace for user-defined attributes. Django uses this namespace for things like 'fields', 'admin', and other important metainfo about a model class besides its attributes. This is important because a model class can interface with the DB in various ways, and can have different CRUD UI's in different ways. If you attributes into that namespace, you'd then have an artificial ban on attributes that happen to be called 'admin' etc. ... that seems inelegant.

### Robby September 15, 2005 at 5:39 a.m.

Does Django's database APIs provide support for transactions?
A quick review of the docs did not seem to clarify this point.

### Simon Willison September 15, 2005 at 7:30 a.m.

Robby: Transaction support is under discussion in http://code.djangoproject.com/ticket/9

## Post a comment

Note: Please only use the comments for questions/critcisms/suggestions on the docs; if you experience errors please file a ticket, ask in the IRC channel, or post to the django-users list. Comments will be periodically reviewed, integrated into the documentation proper, and removed.

Your name:

Comment: