2011-05-27 18:49:47 +08:00
|
|
|
============
|
2012-02-18 04:03:40 +08:00
|
|
|
Coding style
|
2011-05-27 18:49:47 +08:00
|
|
|
============
|
|
|
|
|
|
|
|
Please follow these coding standards when writing code for inclusion in Django.
|
|
|
|
|
2016-06-20 05:08:41 +08:00
|
|
|
.. _coding-style-python:
|
|
|
|
|
2011-05-27 18:49:47 +08:00
|
|
|
Python style
|
2016-01-03 18:56:22 +08:00
|
|
|
============
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2015-04-28 12:12:31 +08:00
|
|
|
* Please conform to the indentation style dictated in the ``.editorconfig``
|
|
|
|
file. We recommend using a text editor with `EditorConfig`_ support to avoid
|
|
|
|
indentation and whitespace issues. The Python files use 4 spaces for
|
|
|
|
indentation and the HTML files use 2 spaces.
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* Unless otherwise specified, follow :pep:`8`.
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2013-11-08 09:11:37 +08:00
|
|
|
Use `flake8`_ to check for problems in this area. Note that our ``setup.cfg``
|
|
|
|
file contains some excluded files (deprecated modules we don't care about
|
|
|
|
cleaning up and some third-party code that Django vendors) as well as some
|
|
|
|
excluded errors that we don't consider as gross violations. Remember that
|
|
|
|
:pep:`8` is only a guide, so respect the style of the surrounding code as a
|
|
|
|
primary goal.
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2014-09-04 20:15:09 +08:00
|
|
|
An exception to :pep:`8` is our rules on line lengths. Don't limit lines of
|
|
|
|
code to 79 characters if it means the code looks significantly uglier or is
|
|
|
|
harder to read. We allow up to 119 characters as this is the width of GitHub
|
|
|
|
code review; anything longer requires horizontal scrolling which makes review
|
|
|
|
more difficult. This check is included when you run ``flake8``. Documentation,
|
|
|
|
comments, and docstrings should be wrapped at 79 characters, even though
|
|
|
|
:pep:`8` suggests 72.
|
2012-02-18 04:03:40 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* Use four spaces for indentation.
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2017-06-13 03:39:09 +08:00
|
|
|
* Use four space hanging indentation rather than vertical alignment::
|
|
|
|
|
|
|
|
raise AttributeError(
|
2018-11-02 22:59:12 +08:00
|
|
|
'Here is a multiline error message '
|
2017-06-13 03:39:09 +08:00
|
|
|
'shortened for clarity.'
|
|
|
|
)
|
|
|
|
|
|
|
|
Instead of::
|
|
|
|
|
2018-11-02 22:59:12 +08:00
|
|
|
raise AttributeError('Here is a multiline error message '
|
2017-06-13 03:39:09 +08:00
|
|
|
'shortened for clarity.')
|
|
|
|
|
|
|
|
This makes better use of space and avoids having to realign strings if the
|
|
|
|
length of the first line changes.
|
|
|
|
|
2018-07-18 23:24:07 +08:00
|
|
|
* Use single quotes for strings, or a double quote if the string contains a
|
2017-06-13 03:39:09 +08:00
|
|
|
single quote. Don't waste time doing unrelated refactoring of existing code
|
|
|
|
to conform to this style.
|
|
|
|
|
|
|
|
* Avoid use of "we" in comments, e.g. "Loop over" rather than "We loop over".
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* Use underscores, not camelCase, for variable, function and method names
|
2016-07-13 22:15:39 +08:00
|
|
|
(i.e. ``poll.get_unique_voters()``, not ``poll.getUniqueVoters()``).
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* Use ``InitialCaps`` for class names (or for factory functions that
|
|
|
|
return classes).
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2017-03-21 22:12:41 +08:00
|
|
|
* In docstrings, follow the style of existing docstrings and :pep:`257`.
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2018-04-28 05:18:15 +08:00
|
|
|
* In tests, use
|
|
|
|
:meth:`~django.test.SimpleTestCase.assertRaisesMessage` and
|
|
|
|
:meth:`~django.test.SimpleTestCase.assertWarnsMessage`
|
|
|
|
instead of :meth:`~unittest.TestCase.assertRaises` and
|
|
|
|
:meth:`~unittest.TestCase.assertWarns` so you can check the
|
|
|
|
exception or warning message. Use :meth:`~unittest.TestCase.assertRaisesRegex`
|
|
|
|
and :meth:`~unittest.TestCase.assertWarnsRegex` only if you need regular
|
|
|
|
expression matching.
|
2015-12-23 07:19:29 +08:00
|
|
|
|
2019-12-22 18:23:46 +08:00
|
|
|
Use :meth:`assertIs(…, True/False)<unittest.TestCase.assertIs>` for testing
|
|
|
|
boolean values, rather than :meth:`~unittest.TestCase.assertTrue` and
|
|
|
|
:meth:`~unittest.TestCase.assertFalse`, so you can check the actual boolean
|
|
|
|
value, not the truthiness of the expression.
|
|
|
|
|
2016-10-27 15:53:39 +08:00
|
|
|
* In test docstrings, state the expected behavior that each test demonstrates.
|
|
|
|
Don't include preambles such as "Tests that" or "Ensures that".
|
|
|
|
|
|
|
|
Reserve ticket references for obscure issues where the ticket has additional
|
|
|
|
details that can't be easily described in docstrings or comments. Include the
|
|
|
|
ticket number at the end of a sentence like this::
|
|
|
|
|
|
|
|
def test_foo():
|
|
|
|
"""
|
|
|
|
A test docstring looks like this (#123456).
|
|
|
|
"""
|
|
|
|
...
|
|
|
|
|
2016-06-20 05:08:41 +08:00
|
|
|
.. _coding-style-imports:
|
|
|
|
|
2014-11-18 06:35:54 +08:00
|
|
|
Imports
|
2016-01-03 18:56:22 +08:00
|
|
|
=======
|
2014-11-18 06:35:54 +08:00
|
|
|
|
|
|
|
* Use `isort <https://github.com/timothycrosley/isort#readme>`_ to automate
|
|
|
|
import sorting using the guidelines below.
|
|
|
|
|
|
|
|
Quick start:
|
|
|
|
|
2018-01-21 01:38:48 +08:00
|
|
|
.. console::
|
2014-11-18 06:35:54 +08:00
|
|
|
|
2019-04-14 23:02:36 +08:00
|
|
|
$ python -m pip install isort
|
2014-11-18 06:35:54 +08:00
|
|
|
$ isort -rc .
|
|
|
|
|
|
|
|
This runs ``isort`` recursively from your current directory, modifying any
|
|
|
|
files that don't conform to the guidelines. If you need to have imports out
|
|
|
|
of order (to avoid a circular import, for example) use a comment like this::
|
|
|
|
|
|
|
|
import module # isort:skip
|
|
|
|
|
|
|
|
* Put imports in these groups: future, standard library, third-party libraries,
|
|
|
|
other Django components, local Django component, try/excepts. Sort lines in
|
|
|
|
each group alphabetically by the full module name. Place all ``import module``
|
2015-08-22 18:39:38 +08:00
|
|
|
statements before ``from module import objects`` in each section. Use absolute
|
|
|
|
imports for other Django components and relative imports for local components.
|
2014-11-18 06:35:54 +08:00
|
|
|
|
|
|
|
* On each line, alphabetize the items with the upper case items grouped before
|
2018-09-25 22:30:18 +08:00
|
|
|
the lowercase items.
|
2014-11-18 06:35:54 +08:00
|
|
|
|
|
|
|
* Break long lines using parentheses and indent continuation lines by 4 spaces.
|
|
|
|
Include a trailing comma after the last import and put the closing
|
|
|
|
parenthesis on its own line.
|
|
|
|
|
|
|
|
Use a single blank line between the last import and any module level code,
|
|
|
|
and use two blank lines above the first function or class.
|
|
|
|
|
|
|
|
For example (comments are for explanatory purposes only):
|
|
|
|
|
2018-09-11 01:00:34 +08:00
|
|
|
.. code-block:: python
|
|
|
|
:caption: django/contrib/admin/example.py
|
2014-11-18 06:35:54 +08:00
|
|
|
|
|
|
|
# future
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
# standard library
|
|
|
|
import json
|
|
|
|
from itertools import chain
|
|
|
|
|
|
|
|
# third-party
|
|
|
|
import bcrypt
|
|
|
|
|
|
|
|
# Django
|
|
|
|
from django.http import Http404
|
|
|
|
from django.http.response import (
|
|
|
|
Http404, HttpResponse, HttpResponseNotAllowed, StreamingHttpResponse,
|
|
|
|
cookie,
|
|
|
|
)
|
|
|
|
|
|
|
|
# local Django
|
|
|
|
from .models import LogEntry
|
|
|
|
|
|
|
|
# try/except
|
|
|
|
try:
|
2016-10-08 09:06:49 +08:00
|
|
|
import yaml
|
2014-11-18 06:35:54 +08:00
|
|
|
except ImportError:
|
2016-10-08 09:06:49 +08:00
|
|
|
yaml = None
|
2014-11-18 06:35:54 +08:00
|
|
|
|
|
|
|
CONSTANT = 'foo'
|
|
|
|
|
|
|
|
|
2017-06-26 22:30:31 +08:00
|
|
|
class Example:
|
2014-11-18 06:35:54 +08:00
|
|
|
# ...
|
|
|
|
|
|
|
|
* Use convenience imports whenever available. For example, do this::
|
|
|
|
|
2016-01-09 19:40:08 +08:00
|
|
|
from django.views import View
|
2014-11-18 06:35:54 +08:00
|
|
|
|
|
|
|
instead of::
|
|
|
|
|
|
|
|
from django.views.generic.base import View
|
|
|
|
|
2011-05-27 18:49:47 +08:00
|
|
|
Template style
|
2016-01-03 18:56:22 +08:00
|
|
|
==============
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* In Django template code, put one (and only one) space between the curly
|
|
|
|
brackets and the tag contents.
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Do this:
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
.. code-block:: html+django
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
{{ foo }}
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Don't do this:
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
.. code-block:: html+django
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
{{foo}}
|
2011-05-27 18:49:47 +08:00
|
|
|
|
|
|
|
View style
|
2016-01-03 18:56:22 +08:00
|
|
|
==========
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* In Django views, the first parameter in a view function should be called
|
|
|
|
``request``.
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Do this::
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
def my_view(request, foo):
|
|
|
|
# ...
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Don't do this::
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
def my_view(req, foo):
|
|
|
|
# ...
|
2011-05-27 18:49:47 +08:00
|
|
|
|
|
|
|
Model style
|
2016-01-03 18:56:22 +08:00
|
|
|
===========
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* Field names should be all lowercase, using underscores instead of
|
|
|
|
camelCase.
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Do this::
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
class Person(models.Model):
|
|
|
|
first_name = models.CharField(max_length=20)
|
|
|
|
last_name = models.CharField(max_length=40)
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Don't do this::
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
class Person(models.Model):
|
|
|
|
FirstName = models.CharField(max_length=20)
|
|
|
|
Last_Name = models.CharField(max_length=40)
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* The ``class Meta`` should appear *after* the fields are defined, with
|
|
|
|
a single blank line separating the fields and the class definition.
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Do this::
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
class Person(models.Model):
|
|
|
|
first_name = models.CharField(max_length=20)
|
|
|
|
last_name = models.CharField(max_length=40)
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
class Meta:
|
|
|
|
verbose_name_plural = 'people'
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Don't do this::
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
class Person(models.Model):
|
|
|
|
first_name = models.CharField(max_length=20)
|
|
|
|
last_name = models.CharField(max_length=40)
|
|
|
|
class Meta:
|
|
|
|
verbose_name_plural = 'people'
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
Don't do this, either::
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
class Person(models.Model):
|
|
|
|
class Meta:
|
|
|
|
verbose_name_plural = 'people'
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
first_name = models.CharField(max_length=20)
|
|
|
|
last_name = models.CharField(max_length=40)
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* The order of model inner classes and standard methods should be as
|
|
|
|
follows (noting that these are not all required):
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* All database fields
|
|
|
|
* Custom manager attributes
|
|
|
|
* ``class Meta``
|
|
|
|
* ``def __str__()``
|
|
|
|
* ``def save()``
|
|
|
|
* ``def get_absolute_url()``
|
|
|
|
* Any custom methods
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2019-05-15 20:31:42 +08:00
|
|
|
* If ``choices`` is defined for a given model field, define each choice as a
|
|
|
|
list of tuples, with an all-uppercase name as a class attribute on the model.
|
|
|
|
Example::
|
2013-01-24 19:53:32 +08:00
|
|
|
|
|
|
|
class MyModel(models.Model):
|
|
|
|
DIRECTION_UP = 'U'
|
|
|
|
DIRECTION_DOWN = 'D'
|
2019-05-15 20:31:42 +08:00
|
|
|
DIRECTION_CHOICES = [
|
2013-01-24 19:53:32 +08:00
|
|
|
(DIRECTION_UP, 'Up'),
|
|
|
|
(DIRECTION_DOWN, 'Down'),
|
2019-05-15 20:31:42 +08:00
|
|
|
]
|
2011-05-27 18:49:47 +08:00
|
|
|
|
|
|
|
Use of ``django.conf.settings``
|
2016-01-03 18:56:22 +08:00
|
|
|
===============================
|
2011-05-27 18:49:47 +08:00
|
|
|
|
|
|
|
Modules should not in general use settings stored in ``django.conf.settings``
|
|
|
|
at the top level (i.e. evaluated when the module is imported). The explanation
|
|
|
|
for this is as follows:
|
|
|
|
|
|
|
|
Manual configuration of settings (i.e. not relying on the
|
|
|
|
``DJANGO_SETTINGS_MODULE`` environment variable) is allowed and possible as
|
|
|
|
follows::
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
settings.configure({}, SOME_SETTING='foo')
|
|
|
|
|
|
|
|
However, if any setting is accessed before the ``settings.configure`` line,
|
|
|
|
this will not work. (Internally, ``settings`` is a ``LazyObject`` which
|
|
|
|
configures itself automatically when the settings are accessed if it has not
|
|
|
|
already been configured).
|
|
|
|
|
|
|
|
So, if there is a module containing some code as follows::
|
|
|
|
|
|
|
|
from django.conf import settings
|
2015-12-30 23:51:16 +08:00
|
|
|
from django.urls import get_callable
|
2011-05-27 18:49:47 +08:00
|
|
|
|
|
|
|
default_foo_view = get_callable(settings.FOO_VIEW)
|
|
|
|
|
|
|
|
...then importing this module will cause the settings object to be configured.
|
|
|
|
That means that the ability for third parties to import the module at the top
|
|
|
|
level is incompatible with the ability to configure the settings object
|
|
|
|
manually, or makes it very difficult in some circumstances.
|
|
|
|
|
2013-01-01 21:12:42 +08:00
|
|
|
Instead of the above code, a level of laziness or indirection must be used,
|
|
|
|
such as ``django.utils.functional.LazyObject``,
|
|
|
|
``django.utils.functional.lazy()`` or ``lambda``.
|
2011-05-27 18:49:47 +08:00
|
|
|
|
|
|
|
Miscellaneous
|
2016-01-03 18:56:22 +08:00
|
|
|
=============
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* Mark all strings for internationalization; see the :doc:`i18n
|
|
|
|
documentation </topics/i18n/index>` for details.
|
|
|
|
|
|
|
|
* Remove ``import`` statements that are no longer used when you change code.
|
2013-11-08 09:11:37 +08:00
|
|
|
`flake8`_ will identify these imports for you. If an unused import needs to
|
2014-04-23 07:05:14 +08:00
|
|
|
remain for backwards-compatibility, mark the end of with ``# NOQA`` to
|
2013-11-08 09:11:37 +08:00
|
|
|
silence the flake8 warning.
|
2011-10-14 08:12:01 +08:00
|
|
|
|
|
|
|
* Systematically remove all trailing whitespaces from your code as those
|
|
|
|
add unnecessary bytes, add visual clutter to the patches and can also
|
|
|
|
occasionally cause unnecessary merge conflicts. Some IDE's can be
|
|
|
|
configured to automatically remove them and most VCS tools can be set to
|
2013-11-08 09:11:37 +08:00
|
|
|
highlight them in diff outputs.
|
2011-10-14 08:12:01 +08:00
|
|
|
|
|
|
|
* Please don't put your name in the code you contribute. Our policy is to
|
|
|
|
keep contributors' names in the ``AUTHORS`` file distributed with Django
|
|
|
|
-- not scattered throughout the codebase itself. Feel free to include a
|
|
|
|
change to the ``AUTHORS`` file in your patch if you make more than a
|
|
|
|
single trivial change.
|
2011-05-27 18:49:47 +08:00
|
|
|
|
2015-04-28 12:12:31 +08:00
|
|
|
JavaScript style
|
2016-01-03 18:56:22 +08:00
|
|
|
================
|
2015-04-28 12:12:31 +08:00
|
|
|
|
|
|
|
For details about the JavaScript code style used by Django, see
|
|
|
|
:doc:`javascript`.
|
|
|
|
|
2018-09-26 14:48:47 +08:00
|
|
|
.. _editorconfig: https://editorconfig.org/
|
2018-04-18 06:19:29 +08:00
|
|
|
.. _flake8: https://pypi.org/project/flake8/
|