views:

90

answers:

3

In other words, what did you not know when you started with Django that you wish someone had told you?

I've dabbled some in Django but nothing really serious. However, I'm hoping to change that, and I'm wondering if there's any gotchas/shortcomings/whatever that I need to be aware of as I go.

+3  A: 

You can't process data in templates. There are a fair number of questions in SO from people trying to do "real" processing in the templates. The Django template language is -- intentionally -- not JSP or ASP or PHP. It just does presentation and retrieval of values computed in view functions.

If you can't figure out how to do it in the template, you may be trying to do too much. Use the view functions as much as possible.

S.Lott
+4  A: 

Use forms for all data validation. Bulk loading, batch processing, web services, everything.

At first, it seems odd to read a CSV file, populate a Form, and then have the Form validate the data and build the database object. But that's the way it's supposed to work. Use the Forms for all validation no matter what the data source.

S.Lott
IHTP. I use forms for basically every aspect of the business logic, as well as input validation.
TokenMacGuy
+5  A: 

Be aware of specifying absolute paths in your settings.py file. Django doesn't come with an out-of-the-box solution for making everything relative, and you have to employ Python's utilities. The usual solution is something like:

import os
def abspath(file):
    return os.path.join(os.path.dirname(__file__), file).replace('\\','/')

(The last replace part is for Windows runs).

And then use it as follows:

DATABASE_NAME = abspath('mydb.db')
MEDIA_ROOT = abspath('media/')

etc.

Eli Bendersky