Estilo de código

Por favor siga esses padrões quando estiver escrevendo código para inclusão no Django.

Pre-commit checks

pre-commit is a framework for managing pre-commit hooks. These hooks help to identify simple issues before committing code for review. By checking for these issues before code review it allows the reviewer to focus on the change itself, and it can also help to reduce the number of CI runs.

To use the tool, first install pre-commit and then the git hooks:

$ python -m pip install pre-commit
$ pre-commit install
...\> py -m pip install pre-commit
...\> pre-commit install

On the first commit pre-commit will install the hooks, these are installed in their own environments and will take a short while to install on the first run. Subsequent checks will be significantly faster. If an error is found an appropriate error message will be displayed. If the error was with black or isort then the tool will go ahead and fix them for you. Review the changes and re-stage for commit if you are happy with them.

Estilo Python

  • All files should be formatted using the black auto-formatter. This will be run by pre-commit if that is configured.

  • The project repository includes an .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.

  • Exceto quando indicado explicitamente, sempre siga a PEP 8.

    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.

    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 88 characters as this is the line length used by black. This check is included when you run flake8. Documentation, comments, and docstrings should be wrapped at 79 characters, even though PEP 8 suggests 72.

  • String variable interpolation may use %-formatting, f-strings, or str.format() as appropriate, with the goal of maximizing code readability.

    Final judgments of readability are left to the Merger’s discretion. As a guide, f-strings should use only plain variable and property access, with prior local variable assignment for more complex cases:

    # Allowed
    f"hello {user}"
    f"hello {user.name}"
    f"hello {self.user.name}"
    
    # Disallowed
    f"hello {get_user()}"
    f"you are {user.age * 365.25} days old"
    
    # Allowed with local variable assignment
    user = get_user()
    f"hello {user}"
    user_days_old = user.age * 365.25
    f"you are {user_days_old} days old"
    

    f-strings should not be used for any string that may require translation, including error and logging messages. In general format() is more verbose, so the other formatting methods are preferred.

    Don’t waste time doing unrelated refactoring of existing code to adjust the formatting method.

  • Avoid use of “we” in comments, e.g. “Loop over” rather than “We loop over”.

  • Use underscores, not camelCase, for variable, function and method names (i.e. poll.get_unique_voters(), not poll.getUniqueVoters()).

  • Utilize IniciaisMaiúsculas para nomes de classes (ou para funções factory que retornem classes).

  • In docstrings, follow the style of existing docstrings and PEP 257.

  • In tests, use assertRaisesMessage() and assertWarnsMessage() instead of assertRaises() and assertWarns() so you can check the exception or warning message. Use assertRaisesRegex() and assertWarnsRegex() only if you need regular expression matching.

    Use assertIs(…, True/False) for testing boolean values, rather than assertTrue() and assertFalse(), so you can check the actual boolean value, not the truthiness of the expression.

  • 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).
        """
        ...
    

Imports

  • Use isort to automate import sorting using the guidelines below.

    Início Rápido:

    $ python -m pip install "isort >= 5.1.0"
    $ isort .
    
    ...\> py -m pip install "isort >= 5.1.0"
    ...\> isort .
    

    Isso roda o isort recursivamente do seu diretório atual, modificando quaisquer arquivos que não estejam de acordo com as orientações gerais. Se você precisa ter imports fora de ordem (para evitar um import circular, por exemplo) use um comentário como esse:

    import module  # isort:skip
    
  • Coloque os imports nesses grupos: future, bibliotecas padrão, bibliotecas de terceiros, outros componentes Django, componentes Django locais, try/excepts. Ordene as linhas em cada grupo por ordem alfabética pelo nome completo do módulo. Coloque todos os comandos do tipo import module antes de comandos do tipo from module import objects em cada uma das seções. Utilize absolute imports para outros componentes Django e local imports para componentes locais.

  • On each line, alphabetize the items with the upper case items grouped before the lowercase items.

  • Quebre linhas longas usando parênteses e identando as linhas de continuação com 4 espaços. Inclua uma vírgula depois do último import e coloque o parênteses de fechamento em sua própria linha.

    Utilize uma única linha em branco entre o último import e qualquer código a nível de módulo, e utilize duas linhas em branco acima da primeira função ou classe.

    Por exemplo (os comentários existem somente para propósitos de explicação):

    django/contrib/admin/example.py
    # 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:
        import yaml
    except ImportError:
        yaml = None
    
    CONSTANT = "foo"
    
    
    class Example:
        ...
    
  • Use convenience imports whenever available. For example, do this

    from django.views import View
    

    ao invés de:

    from django.views.generic.base import View
    

Estilo dos templates

  • No código de templates Django, coloque um (e somente um) espaço entre as chaves e o conteúdo da tag.

    Faça isso:

    {{ foo }}
    

    Não faça isso:

    {{foo}}
    

Estilo de uma View

  • Nas views do Django, o primeiro parâmetro na função de uma view deve ser chamado request.

    Faça isso:

    def my_view(request, foo):
        ...
    

    Não faça isso:

    def my_view(req, foo):
        ...
    

Estilo de um Model

  • Os nomes dos campos devem ser todos minúsculos, usando underscores ao invés de camelCase.

    Faça isso:

    class Person(models.Model):
        first_name = models.CharField(max_length=20)
        last_name = models.CharField(max_length=40)
    

    Não faça isso:

    class Person(models.Model):
        FirstName = models.CharField(max_length=20)
        Last_Name = models.CharField(max_length=40)
    
  • A classe Meta deve aparecer depois que os campos são declarados, com uma única linha em branco separando os campos da definição da classe.

    Faça isso:

    class Person(models.Model):
        first_name = models.CharField(max_length=20)
        last_name = models.CharField(max_length=40)
    
        class Meta:
            verbose_name_plural = "people"
    

    Não faça isso:

    class Person(models.Model):
        class Meta:
            verbose_name_plural = "people"
    
        first_name = models.CharField(max_length=20)
        last_name = models.CharField(max_length=40)
    
  • A ordem dos modelos internos das classes e dos métodos padrão devem ser como a seguir (tendo em mente que eles não são todos obrigatórios):

    • Todos os campos do banco de dados
    • Atributos customizados do manager
    • classe Meta
    • def __str__()
    • def save()
    • def get_absolute_url()
    • Quaisquer métodos customizados.
  • 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:

    class MyModel(models.Model):
        DIRECTION_UP = "U"
        DIRECTION_DOWN = "D"
        DIRECTION_CHOICES = [
            (DIRECTION_UP, "Up"),
            (DIRECTION_DOWN, "Down"),
        ]
    

Utilização do django.conf.settings

Em geral os módulos não devem utilizar em seu topo configurações armazenadas em django.conf.settings (quando queremos que alguma configuração só seja interpretada quando o módulo é importado, por exemplo). A motivação para isso é a seguinte:

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")

Porém, se qualquer configuração é acessada antes da linha settings.configure, isso não irá funcionar (internamente, settings é um LazyObject que, caso não tenha sido configurado antes, se configura sozinho e automaticamente quando o settings é acessado).

Então, se existir um módulo contendo código como o descrito a seguir:

from django.conf import settings
from django.urls import get_callable

default_foo_view = get_callable(settings.FOO_VIEW)

A importação desse módulo fará com que o objeto settings seja configurado. Isso significa que a abilidade de importar módulos no topo por terceiros é incompatível com a abilidade de configurar o objeto settings manualmente, ou faz com que ela seja extremamente difícil em algumas circunstâncias.

Ao invés do código acima, um nível de laziness ou indireção deve ser usado, tais como django.utils.functional.LazyObject, django.utils.functional.lazy() ou lambda.

Variados

  • Marque todas as strings para internacionalização; veja a documentação i18n para mais detalhes.
  • Remove import statements that are no longer used when you change code. flake8 will identify these imports for you. If an unused import needs to remain for backwards-compatibility, mark the end of with # NOQA to silence the flake8 warning.
  • Remova sistematicamente todos os espaços em branco no final do seu código já que eles adicionam bytes desnecessários, adicionanm poluição visual aos patches e também podem eventualmente ocasionar conflitos de merge. Algumas IDEs podem ser configuradas para automaticamente removê-los e a maioria das ferramentas de versionamento de código podem ser configuradas para exibi-los na geração dos diffs.
  • Por favor não coloque o seu nome no código que você está contribuindo. Nossa política é de manter os nomes de quem contribui no arquivo AUTHORS distribuído com o Django – e não espalhado por todo o código do projeto. Sinta-se à vontade para incluir uma mudança no arquivo AUTHORS no seu patch se você fez mais do que uma mudança trivial.

Estilo JavaScript

Para detalhes sobre o estilo de código JavaScript adotado no Django, veja JavaScript.