| 1 | = Do's and Dont's for Application Writers = |
| 2 | |
| 3 | This is a guide to writing Django applications, the goals |
| 4 | I'm aiming for are: |
| 5 | |
| 6 | * the application should be easy to install for users |
| 7 | * the application should play nice with other applications |
| 8 | |
| 9 | These are the guidelines I would proprose, please comment |
| 10 | and discuss. I hope this can become something everyone in |
| 11 | the Django community can agree on. |
| 12 | |
| 13 | Throughout this document I will need to refer to an imaginary |
| 14 | application for examples; let's call it mnemosyne, and put it |
| 15 | in ibofobi.apps. When I need to refer to paths, I'll use relative |
| 16 | paths rooted at the mnemosyne package directory. |
| 17 | |
| 18 | == Templates == |
| 19 | |
| 20 | * To avoid name clashes with templates from other applications all your templates should |
| 21 | go into {{{mnemosyne/templates/mnemosyne/}}} |
| 22 | * Your templates should all extend a single {{{base}}}-template specific to |
| 23 | your application, in my example it would be {{{mnemosyne/templates/mnemosyne/base.html}}} |
| 24 | and all my templates would start with |
| 25 | {{{ |
| 26 | {% extends "mnemosyne/base" %} |
| 27 | }}} |
| 28 | and the {{{mnemosyne/base}}} template should either be a complete HTML-document, |
| 29 | or (and I think this is preferrable) just be |
| 30 | {{{ |
| 31 | {% extends "base" %} |
| 32 | }}} |
| 33 | (unless you have any pages which should go under the admin, then they |
| 34 | should extend {{{admin/base_site}}}.) |
| 35 | ''Should I ''s/base/base_site/'' to be consistent with the admin-application? |
| 36 | I personally prefer ''{{{bare}}}'' and ''{{{base}}}'' over ''{{{base}}}'' and |
| 37 | ''{{{base_site}}}''; they are more concise and they lack the CTS-inducing |
| 38 | underscore.'' |
| 39 | * Your templates should fill in two blocks: {{{title}}} and {{{content}}}. |
| 40 | * You should refer to media-files with a configurable prefix, |
| 41 | in the example a good setting would be {{{MNEMOSYNE_MEDIA_ROOT}}}. |
| 42 | For example: |
| 43 | {{{ |
| 44 | <link rel='stylesheet' href='{{ MNEMOSYNE_MEDIA_ROOT }}/my.css' /> |
| 45 | }}} |
| 46 | * You should refer to pages in your application with either relative links, |
| 47 | or where this is impractical or just plain impossible, with a configurable |
| 48 | prefix. In the example I would go with {{{MNEMOSYNE_ROOT}}}. |
| 49 | * Please, please, please. Try to write templates as correct XHTML. |
| 50 | |
| 51 | == Media files == |
| 52 | |
| 53 | * Your media-files should go into {{{mnemosyne/media/}}}. |
| 54 | ''Should that be ''{{{mnemosyne/media/mnemosyne/}}}''? It would mirror |
| 55 | the templates, and probably looks better in ''{{{Alias}}}''-directives |
| 56 | in Apache-configurations ;)'' |
| 57 | |
| 58 | == Settings == |
| 59 | |
| 60 | * Your application should have decent default values for |
| 61 | {{{MNEMOSYNE_ROOT}}} and {{{MNEMOSYNE_MEDIA_ROOT}}}, for example {{{/mnemosyne}}} |
| 62 | and {{{/media/mnemosyne}}}. |
| 63 | |
| 64 | = Somewhat related stuff = |
| 65 | |
| 66 | * A template for an application {{{setup.py}}}: |
| 67 | {{{ |
| 68 | try: |
| 69 | from setuptools import setup |
| 70 | except ImportError: |
| 71 | from distutils.core import setup |
| 72 | setup(name = "mnemosyne", |
| 73 | author = "Sune Kirkeby", |
| 74 | url = "http://ibofobi.dk/stuff/mnemosyne/", |
| 75 | version = '0.1', |
| 76 | packages = ['ibofobi', 'ibofobi.apps', 'ibofobi.apps.mnemosyne', |
| 77 | 'ibofobi.apps.mnemosyne.models', |
| 78 | 'ibofobi.apps.mnemosyne.views', |
| 79 | 'ibofobi.apps.mnemosyne.urls'], |
| 80 | package_dir = {'': 'src'}, |
| 81 | package_data = {'ibofobi.apps.mnemosyne': ['templates/mnemosyne/*.html', |
| 82 | 'media/*',],}, |
| 83 | # distutils complain about these, anyone know an easy way to silence it? |
| 84 | namespace_packages = ['ibofobi.apps'], |
| 85 | zip_safe = True, |
| 86 | ) |
| 87 | }}} |
| 88 | * Is {{{templates/<app-name>/}}} and {{{media/}}} created by |
| 89 | {{{django-admin startapp}}? Should they be? |
| 90 | * It would be nice if doing things this way was more natural in Django, |
| 91 | than doing it any other way. For example it would be nice if |
| 92 | {{{django.conf.settings}}} was in {{{Context}}} as {{{settings}}}, |
| 93 | so templates would have access to {{{MNEMOSYNE_ROOT}}} and {{{MNEMOSYNE_MEDIA_ROOT}}}. |
| 94 | * A ten-minute walk-through of an application might be good, to make the points in |
| 95 | this document easier to visualize. |
| 96 | |
| 97 | = Comments = |
| 98 | |
| 99 | I would prefer to use {{{django-users}}} for comments and discussions on this |
| 100 | document, wikis have very poor threading :) So, if you have comments, or there's |
| 101 | something you just plain disagree with, please bring it up there. |