coding-style.txt 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. ============
  2. Coding Style
  3. ============
  4. Please follow these coding standards when writing code for inclusion in Django.
  5. Python style
  6. ------------
  7. * Unless otherwise specified, follow :pep:`8`.
  8. You could use a tool like `pep8.py`_ to check for some problems in this
  9. area, but remember that PEP 8 is only a guide, so respect the style of
  10. the surrounding code as a primary goal.
  11. * Use four spaces for indentation.
  12. * Use underscores, not camelCase, for variable, function and method names
  13. (i.e. ``poll.get_unique_voters()``, not ``poll.getUniqueVoters``).
  14. * Use ``InitialCaps`` for class names (or for factory functions that
  15. return classes).
  16. * In docstrings, use "action words" such as::
  17. def foo():
  18. """
  19. Calculates something and returns the result.
  20. """
  21. pass
  22. Here's an example of what not to do::
  23. def foo():
  24. """
  25. Calculate something and return the result.
  26. """
  27. pass
  28. Template style
  29. --------------
  30. * In Django template code, put one (and only one) space between the curly
  31. brackets and the tag contents.
  32. Do this:
  33. .. code-block:: html+django
  34. {{ foo }}
  35. Don't do this:
  36. .. code-block:: html+django
  37. {{foo}}
  38. View style
  39. ----------
  40. * In Django views, the first parameter in a view function should be called
  41. ``request``.
  42. Do this::
  43. def my_view(request, foo):
  44. # ...
  45. Don't do this::
  46. def my_view(req, foo):
  47. # ...
  48. Model style
  49. -----------
  50. * Field names should be all lowercase, using underscores instead of
  51. camelCase.
  52. Do this::
  53. class Person(models.Model):
  54. first_name = models.CharField(max_length=20)
  55. last_name = models.CharField(max_length=40)
  56. Don't do this::
  57. class Person(models.Model):
  58. FirstName = models.CharField(max_length=20)
  59. Last_Name = models.CharField(max_length=40)
  60. * The ``class Meta`` should appear *after* the fields are defined, with
  61. a single blank line separating the fields and the class definition.
  62. Do this::
  63. class Person(models.Model):
  64. first_name = models.CharField(max_length=20)
  65. last_name = models.CharField(max_length=40)
  66. class Meta:
  67. verbose_name_plural = 'people'
  68. Don't do this::
  69. class Person(models.Model):
  70. first_name = models.CharField(max_length=20)
  71. last_name = models.CharField(max_length=40)
  72. class Meta:
  73. verbose_name_plural = 'people'
  74. Don't do this, either::
  75. class Person(models.Model):
  76. class Meta:
  77. verbose_name_plural = 'people'
  78. first_name = models.CharField(max_length=20)
  79. last_name = models.CharField(max_length=40)
  80. * The order of model inner classes and standard methods should be as
  81. follows (noting that these are not all required):
  82. * All database fields
  83. * Custom manager attributes
  84. * ``class Meta``
  85. * ``def __unicode__()``
  86. * ``def __str__()``
  87. * ``def save()``
  88. * ``def get_absolute_url()``
  89. * Any custom methods
  90. * If ``choices`` is defined for a given model field, define the choices as
  91. a tuple of tuples, with an all-uppercase name, either near the top of
  92. the model module or just above the model class. Example::
  93. GENDER_CHOICES = (
  94. ('M', 'Male'),
  95. ('F', 'Female'),
  96. )
  97. Use of ``django.conf.settings``
  98. -------------------------------
  99. Modules should not in general use settings stored in ``django.conf.settings``
  100. at the top level (i.e. evaluated when the module is imported). The explanation
  101. for this is as follows:
  102. Manual configuration of settings (i.e. not relying on the
  103. ``DJANGO_SETTINGS_MODULE`` environment variable) is allowed and possible as
  104. follows::
  105. from django.conf import settings
  106. settings.configure({}, SOME_SETTING='foo')
  107. However, if any setting is accessed before the ``settings.configure`` line,
  108. this will not work. (Internally, ``settings`` is a ``LazyObject`` which
  109. configures itself automatically when the settings are accessed if it has not
  110. already been configured).
  111. So, if there is a module containing some code as follows::
  112. from django.conf import settings
  113. from django.core.urlresolvers import get_callable
  114. default_foo_view = get_callable(settings.FOO_VIEW)
  115. ...then importing this module will cause the settings object to be configured.
  116. That means that the ability for third parties to import the module at the top
  117. level is incompatible with the ability to configure the settings object
  118. manually, or makes it very difficult in some circumstances.
  119. Instead of the above code, a level of laziness or indirection must be used,
  120. such as `django.utils.functional.LazyObject``, ``django.utils.functional.lazy``
  121. or ``lambda``.
  122. Miscellaneous
  123. -------------
  124. * Mark all strings for internationalization; see the :doc:`i18n
  125. documentation </topics/i18n/index>` for details.
  126. * Please don't put your name in the code you contribute. Our policy is to
  127. keep contributors' names in the ``AUTHORS`` file distributed with Django
  128. -- not scattered throughout the codebase itself. Feel free to include a
  129. change to the ``AUTHORS`` file in your patch if you make more than a
  130. single trivial change.
  131. .. _pep8.py: http://pypi.python.org/pypi/pep8/