Requirements

  • Use Python >= 2.5 (sqlite and others usefull libs are included), on Mac prefer the Macports python, cause addon's installations will be simpler and your system will remains clean (installation in /opt/local)
  • Install ipython It will provide you a better manage.py shell
  • Install docutils, it will generate you a usefull documentation, that you can browse via the Documentation link in the Django admin view
  • Django sources: Follow django trunk for a maximum of time, the API is becoming fairly stable, no need to work on a fixed version at the early parts of your project


Charset

  • Since > 0.96 Django support unicode everywhere use it: Start all your py files with # -*- coding: utf-8 -*-, utf-8 is the solution to most charsets problems, abuse it (All my visitors are using ASCII, is not an excuse, except if you live in 1980)
  • Only define _unicode_ model methods and let Django deal with _str_ alone See __unicode__ doc
  • Think in unicode: A string is no more a "my text" but u"my text", _unicode_ method needs return u"Something"


Models

  • Only use null=True for non-string fields such as integers, booleans and dates: Avoid using null on string-based fields such as CharField and TextField unless you have an excellent reason. See null model doc
  • Use fixtures to reimport data and test your apps manage.py dumpdata --indent=4 > initial.json and manage.py loaddata initial, don't spend time to reinsert data everyday with the admin
  • Spend a lot of time on your model, it describes your data, enforce every field, for example use null=True only where needed
  • Handle static choices the right way, use IntergerField for choices list
  • Storing additional information about users: At this time the solution in not to subclass User but to set AUTH_PROFILE_MODULE in your settings and let play with get_profile() See addional informations about user
  • Use the admin to test your model, what could be better than a web browsing to validate your model ?


Views

  • Create a urls.py per application and call it from the root urls.py: (r'^myapp/', include('myproject.myapp.urls'))
  • use @transaction.commit_on_success decorators when you are inserting data, the action to commit modifications in db can be associated to the return code of your view (200 ok django will commit), It makes your inserted data more consistent, See my earlier post for a little case study


Debug

  • Put a assert False in your code (in view) it will act as a breakpoint, you can now browse the local vars in the debug page
  • Use template language and {% if debug %} to put some sql queries debug facilties in your views (change INTERNAL_IPS in your settings.py if you are using a remote server)
    If you don't have a template at the moment you need to debug your sql, use this anywhere in your code and browse the debug page
        from django.db import connection
        a = connection.queries
        assert False


Production

  • Create a settings-dev.py and a settings-production.py then link settings.py to the one you need
  • Regularly test your code against the targeted production platform: Sqlite is not Mysql nor Postgres, Apache, mod_python, mod_wsgi are not the embedded server, they comes at a price: some side effects


I hope we will update this list with new tips.

UPDATE: 08/02/20 Fix Malcolm recommandations, add Storing additional information about users
UPDATE: 08/06/20 add assert False tip