design-philosophies.txt 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. .. _misc-design-philosophies:
  2. ===================
  3. Design philosophies
  4. ===================
  5. This document explains some of the fundamental philosophies Django's developers
  6. have used in creating the framework. Its goal is to explain the past and guide
  7. the future.
  8. Overall
  9. =======
  10. .. _loose-coupling:
  11. Loose coupling
  12. --------------
  13. .. index:: coupling; loose
  14. A fundamental goal of Django's stack is `loose coupling and tight cohesion`_.
  15. The various layers of the framework shouldn't "know" about each other unless
  16. absolutely necessary.
  17. For example, the template system knows nothing about Web requests, the database
  18. layer knows nothing about data display and the view system doesn't care which
  19. template system a programmer uses.
  20. Although Django comes with a full stack for convenience, the pieces of the
  21. stack are independent of another wherever possible.
  22. .. _`loose coupling and tight cohesion`: http://c2.com/cgi/wiki?CouplingAndCohesion
  23. .. _less-code:
  24. Less code
  25. ---------
  26. Django apps should use as little code as possible; they should lack boilerplate.
  27. Django should take full advantage of Python's dynamic capabilities, such as
  28. introspection.
  29. .. _quick-development:
  30. Quick development
  31. -----------------
  32. The point of a Web framework in the 21st century is to make the tedious aspects
  33. of Web development fast. Django should allow for incredibly quick Web
  34. development.
  35. .. _dry:
  36. Don't repeat yourself (DRY)
  37. ---------------------------
  38. .. index::
  39. single: DRY
  40. single: Don't repeat yourself
  41. Every distinct concept and/or piece of data should live in one, and only one,
  42. place. Redundancy is bad. Normalization is good.
  43. The framework, within reason, should deduce as much as possible from as little
  44. as possible.
  45. .. seealso::
  46. The `discussion of DRY on the Portland Pattern Repository`__
  47. __ http://c2.com/cgi/wiki?DontRepeatYourself
  48. .. _explicit-is-better-than-implicit:
  49. Explicit is better than implicit
  50. --------------------------------
  51. This, a `core Python principle`_, means Django shouldn't do too much "magic."
  52. Magic shouldn't happen unless there's a really good reason for it. Magic is
  53. worth using only if it creates a huge convenience unattainable in other ways,
  54. and it isn't implemented in a way that confuses developers who are trying to
  55. learn how to use the feature.
  56. .. _`core Python principle`: http://www.python.org/dev/peps/pep-0020/
  57. .. _consistency:
  58. Consistency
  59. -----------
  60. The framework should be consistent at all levels. Consistency applies to
  61. everything from low-level (the Python coding style used) to high-level (the
  62. "experience" of using Django).
  63. Models
  64. ======
  65. Explicit is better than implicit
  66. --------------------------------
  67. Fields shouldn't assume certain behaviors based solely on the name of the
  68. field. This requires too much knowledge of the system and is prone to errors.
  69. Instead, behaviors should be based on keyword arguments and, in some cases, on
  70. the type of the field.
  71. Include all relevant domain logic
  72. ---------------------------------
  73. Models should encapsulate every aspect of an "object," following Martin
  74. Fowler's `Active Record`_ design pattern.
  75. This is why both the data represented by a model and information about
  76. it (its human-readable name, options like default ordering, etc.) are
  77. defined in the model class; all the information needed to understand a
  78. given model should be stored *in* the model.
  79. .. _`Active Record`: http://www.martinfowler.com/eaaCatalog/activeRecord.html
  80. Database API
  81. ============
  82. The core goals of the database API are:
  83. SQL efficiency
  84. --------------
  85. It should execute SQL statements as few times as possible, and it should
  86. optimize statements internally.
  87. This is why developers need to call ``save()`` explicitly, rather than the
  88. framework saving things behind the scenes silently.
  89. This is also why the ``select_related()`` ``QuerySet`` method exists. It's an
  90. optional performance booster for the common case of selecting "every related
  91. object."
  92. Terse, powerful syntax
  93. ----------------------
  94. The database API should allow rich, expressive statements in as little syntax
  95. as possible. It should not rely on importing other modules or helper objects.
  96. Joins should be performed automatically, behind the scenes, when necessary.
  97. Every object should be able to access every related object, systemwide. This
  98. access should work both ways.
  99. Option to drop into raw SQL easily, when needed
  100. -----------------------------------------------
  101. The database API should realize it's a shortcut but not necessarily an
  102. end-all-be-all. The framework should make it easy to write custom SQL -- entire
  103. statements, or just custom ``WHERE`` clauses as custom parameters to API calls.
  104. URL design
  105. ==========
  106. Loose coupling
  107. --------------
  108. URLs in a Django app should not be coupled to the underlying Python code. Tying
  109. URLs to Python function names is a Bad And Ugly Thing.
  110. Along these lines, the Django URL system should allow URLs for the same app to
  111. be different in different contexts. For example, one site may put stories at
  112. ``/stories/``, while another may use ``/news/``.
  113. Infinite flexibility
  114. --------------------
  115. URLs should be as flexible as possible. Any conceivable URL design should be
  116. allowed.
  117. Encourage best practices
  118. ------------------------
  119. The framework should make it just as easy (or even easier) for a developer to
  120. design pretty URLs than ugly ones.
  121. File extensions in Web-page URLs should be avoided.
  122. Vignette-style commas in URLs deserve severe punishment.
  123. .. _definitive-urls:
  124. Definitive URLs
  125. ---------------
  126. .. index:: urls; definitive
  127. Technically, ``foo.com/bar`` and ``foo.com/bar/`` are two different URLs, and
  128. search-engine robots (and some Web traffic-analyzing tools) would treat them as
  129. separate pages. Django should make an effort to "normalize" URLs so that
  130. search-engine robots don't get confused.
  131. This is the reasoning behind the :setting:`APPEND_SLASH` setting.
  132. Template system
  133. ===============
  134. .. _separation-of-logic-and-presentation:
  135. Separate logic from presentation
  136. --------------------------------
  137. We see a template system as a tool that controls presentation and
  138. presentation-related logic -- and that's it. The template system shouldn't
  139. support functionality that goes beyond this basic goal.
  140. If we wanted to put everything in templates, we'd be using PHP. Been there,
  141. done that, wised up.
  142. Discourage redundancy
  143. ---------------------
  144. The majority of dynamic Web sites use some sort of common sitewide design --
  145. a common header, footer, navigation bar, etc. The Django template system should
  146. make it easy to store those elements in a single place, eliminating duplicate
  147. code.
  148. This is the philosophy behind :ref:`template inheritance
  149. <template-inheritance>`.
  150. Be decoupled from HTML
  151. ----------------------
  152. The template system shouldn't be designed so that it only outputs HTML. It
  153. should be equally good at generating other text-based formats, or just plain
  154. text.
  155. XML should not be used for template languages
  156. ---------------------------------------------
  157. .. index:: xml; suckiness of
  158. Using an XML engine to parse templates introduces a whole new world of human
  159. error in editing templates -- and incurs an unacceptable level of overhead in
  160. template processing.
  161. Assume designer competence
  162. --------------------------
  163. The template system shouldn't be designed so that templates necessarily are
  164. displayed nicely in WYSIWYG editors such as Dreamweaver. That is too severe of
  165. a limitation and wouldn't allow the syntax to be as nice as it is. Django
  166. expects template authors are comfortable editing HTML directly.
  167. Treat whitespace obviously
  168. --------------------------
  169. The template system shouldn't do magic things with whitespace. If a template
  170. includes whitespace, the system should treat the whitespace as it treats text
  171. -- just display it. Any whitespace that's not in a template tag should be
  172. displayed.
  173. Don't invent a programming language
  174. -----------------------------------
  175. The template system intentionally doesn't allow the following:
  176. * Assignment to variables
  177. * Advanced logic
  178. The goal is not to invent a programming language. The goal is to offer just
  179. enough programming-esque functionality, such as branching and looping, that is
  180. essential for making presentation-related decisions.
  181. The Django template system recognizes that templates are most often written by
  182. *designers*, not *programmers*, and therefore should not assume Python
  183. knowledge.
  184. Safety and security
  185. -------------------
  186. The template system, out of the box, should forbid the inclusion of malicious
  187. code -- such as commands that delete database records.
  188. This is another reason the template system doesn't allow arbitrary Python code.
  189. Extensibility
  190. -------------
  191. The template system should recognize that advanced template authors may want
  192. to extend its technology.
  193. This is the philosophy behind custom template tags and filters.
  194. Views
  195. =====
  196. Simplicity
  197. ----------
  198. Writing a view should be as simple as writing a Python function. Developers
  199. shouldn't have to instantiate a class when a function will do.
  200. Use request objects
  201. -------------------
  202. Views should have access to a request object -- an object that stores metadata
  203. about the current request. The object should be passed directly to a view
  204. function, rather than the view function having to access the request data from
  205. a global variable. This makes it light, clean and easy to test views by passing
  206. in "fake" request objects.
  207. Loose coupling
  208. --------------
  209. A view shouldn't care about which template system the developer uses -- or even
  210. whether a template system is used at all.
  211. Differentiate between GET and POST
  212. ----------------------------------
  213. GET and POST are distinct; developers should explicitly use one or the other.
  214. The framework should make it easy to distinguish between GET and POST data.