Context Navigation

This is a work in progress, answering questions that do indeed get asked frequently on the ​Django IRC channel (#django on irc.freenode.net). Feel free to contribute, but try to keep it clear and concise. Please don't editorialize.

I'm trying to install Django on Windows and something is weird.

What should I use for development -- the built-in server, mod_python, FastCGI?

It's generally easiest to use the ​built-in development server for development, since it automatically reloads your Python source files when it detects changes. Apache needs to be restarted to see changes to your source files (unless you set MaxRequestsPerChild 1, which you should do with caution since it is not suitable for production).

Some prefer to replicate the production environment as closely as possible, meaning that if their deployed project uses mod_python then their development server does as well.

What database should I use?

For development, most people find SQLite to be fastest and simplest to run with -- just make sure the database file and its directory are writeable by the owner of the web server process. For production, PostgreSQL and MySQL are the most thoroughly-tested of the databases Django supports, but it's best to choose based on the needs of your applications; for example, applications which do very little writing of data to the DB will enjoy the speed of SQLite, but applications which involve many complex queries or which require robust concurrent-write features like transaction isolation will probably want to look at Postgres or MySQL (and, of course, MySQL is often handy simply because many shared hosting providers have it set up by default).

Troubleshooting

General problem-solving advice: Test the things you think are true until you find the one that isn't.

"It doesn't work!" or "I got an error!"

If you want good help you'll need to give a little more information. Keep in mind that we probably know little if anything about your project, your level of experience with Python and Django, etc.

Did it work before? Or is this something you're trying for the first time? Does it raise an error? Die silently? Give unexpected output?

If you want help with an error, try to give us 1) the code that produced the error and 2) the error traceback itself.

When asking for help, make sure you describe 1) what you did, 2) what you expected to happen, and 3) what actually happened.

​Pasting your code is often helpful. Don't forget to set the syntax when you paste so that the proper colorizing is applied -- that makes it easier for us to read your stuff. The Django debug page has a handy button for automatically sharing traceback code.

I'm using the development version (via Subversion) and when I ran "svn up" a bunch of stuff broke!

Confusingly, though the MEDIA_* settings in your settings.py file refer to your media files, not the Admin app's media files, the default path for Admin app media (ADMIN_MEDIA_PREFIX) is set to "/media/". Many Django users change this to "/admin_media/" or "/adminmedia/" to reduce potential confusion.

Why isn't my template tag or filter working?

Of course, you've read the ​documentation, right?
Here's a brief list of things to check first before asking:

your filter/tag should be in a file named something like [yourfilterlibraryname].py (e.g. if it was myfilter.py, you'll use {% load myfilter %} in the template)

that file exists in a directory named templatetags and this directory is sitting inside of an app directory (e.g. .../projectdir/appdir/templatetags)

an empty file named __init__.py also exists in the templatetags directory (this makes the directory into a python module which can be imported)

in your project settings.py, the application where the templatetags directory is contained is in INSTALLED_APPS

each filter or tag in your library is decorated correctly (see docs)

neither your library nor anything it imports raises any exceptions (for example, if your library tries to import something that's not installed, and so raises an ImportError, that will prevent Django from seeing it)

restart the webserver (yes, even if you're using runserver)

How to do Stuff

How do I create a subclass of an existing model?

If you're using model inheritance (aka model subclassing), make sure you've ​read the documentation and understand the difference between the abstract base class and multi-table inheritance options. The latter can be confusing to newcomers because an instance of a parent class will also contain, on an attribute named after a child class, that same object represented as an instance of the child class. Got that?

How do I customise the admin interface so all logged-in users can use it without screwing up anything?

The admin interface is designed for use by trusted site staff, not by any user -- if you don't trust a user with the level of access the admin application provides, you'll need to provide non-admin views for the actions you'd like to allow them to take.

I want to have some code run when the server/application starts. How do I do that?

Both mod_python and FastCGI are structured in such a way that there's no such thing as "application startup" or "server startup"; the best solution is to place your "startup" code somewhere that's guaranteed to be imported early on in the request/response cycle (the __init__.py file of your project, or of a specific application you're using, can be a good place, because Python will execute code found there the first time it has to import the module; just be aware that referencing the same module in different ways, say by doing from myproject.myapp import foo in one place, and from myapp import foo in another, will cause that code to be executed once for each different way you import it).

Do I have to hard-code my media URL in all my templates for CSS, images and Javascript?

No; you can use {{ MEDIA_URL }}. If you're using generic views, you can use {{ MEDIA_URL }} without changing anything.

How do I use Django in a shell script?

Can I use the Date/Time picker JavaScript from the Admin in my own app?

Short answer: No.

Long answer: It is possible, but requires reusing the Admin JS and CSS, and is more trouble than it is worth. Most JavaScript frameworks (such as ​YUI, ​Dojo, ​jQuery, and ​Prototype) provide similar functionality, either out of the box or through plugins. Additionally, there are many JavaScript snippets available across the Web.

I want to repeat a bit of dynamic information (eg from a database) on many views. Do I have to change every view?

If I change my model, will manage.py syncdb update my database table?

No, you'll need to manually change your database table. If you use manage.py sqlall on your app to produce a SQL file before editing your models, you can run it again afterwards and use the difference between the two to see what you need to change in the database.

Resources, Tools, and Code

Which runs faster, X or Y?

This is a tempting question to ask in hopes of getting a quick answer on the "best" way to do something. Unfortunately, the answer is generally "profile your app and see". Performance tuning always starts with a baseline. If you haven't measured current performance to get a baseline, you aren't in a position to do much with the answer to the question anyway.