Django Design Philosophy Simplified

This post talks about following few of the design standards in your django project so that you end up writing code that is prettier, more readable and complies to the best practices. There are few recommendations and conventions that should be followed in each of your models, settings and templates.

Templates:

  1. There should be one base.html at the project level, and one base.html at each of the app levels. The app level base.html should extend the project level base.html.

  2. The variable in templates should have spaces between the name and bracket. Eg. {{ variable }} and NOT {{variable}}

Settings File:

  1. Use relative path in settings.py instead of absolute path eg. for referencing to static files directory you should go by: import os PROJECT_DIR=os.path.dirname(__file__) STATIC_ROOT = os.path.join(PROJECT_DIR,'static_media/') and NOT STATIC_ROOT = "/home/media/media.lawrence.com/static_media/"

  2. Sensitive things as database password, API keys etc. should be put in localsettings

  3. Custom Middlewares and Context Processors should be in a try catch block.

Urls:

  1. Each of the urls should have a name, corresponding to the view name

  2. There should be one urls.py at the project level, and one urls.py at each app level. The project level urls.py should include each of the urls.py under a prefix.

  3. Use of slugs is recommended instead of primary keys.

Models:

  1. The database fields should be names as first_name and NOT FirstName.

  2. The database table names(class names) should be named as ClassNames and not class_names

  3. There should be a unicode method for each of the classes.

  4. Use of permalink decorator should be preferred to get_absolute_url

  5. The model of inner classes should be in the following order:

    • db fields
    • managers
    • meta class
    • db fields
    • managers
    • unicode
    • str
    • save
    • absolute_url

Further Reading:

  1. django best practises

  2. django design philosophy

Comments

blog comments powered by Disqus