Django official code style guide learning summary

Python style

PEP 8 governor limits a maximum of 79 characters, but Django allows up to 119 characters (this is the code review tool allows GitHub width). LIMITATIONS PEP 8, annotation, the docstring of line length is 72 characters, Django to limit them to 79 characters.

Use Flake8 to check the code quality.

  • Each indent 4 spaces

  • Variables, functions, method names with an underscore _, rather than camelCase , as should be written poll.get_unique_voters(), but instead of writingpoll.getUniqueVoters()

  • Class name and returns the class factory function name to use InitiaCaps form

  • Easy to use style as much as possible import, such as the use from django.views.generic import View, rather than usingfrom django.views.generic.base import View

  • In docstring, use the "action statement", such as:

# 这样使用:
def foo():
    """
    Calculates something and returns the result.
    """
    pass
# 不要这样使用:
def foo():
    """
    Calculate something and return the result.
    """
    pass

Template style

  • Both sides of the label content and only one space, such as {{ foo }}, but not{{foo}}

View style

  • The first view function be requestsuch def my_view(request, foo):, rather than usingdef my_view(req, foo):

Data model style

  • Data item names should be all lowercase, with an underscore instead camelCase , such as:
# 正确的风格:
class Person(models.Model):
    first_name = models.CharField(max_length=20)
    last_name = models.CharField(max_length=40)
# 错误的风格:
class Person(models.Model):
    FirstName = models.CharField(max_length=20)
    Last_Name = models.CharField(max_length=40)
  • class Meta It should be placed after the data item is defined, separated by a blank line between, such as:
# 正确的风格:
class Person(models.Model):
    first_name = models.CharField(max_length=20)
    last_name = models.CharField(max_length=40)

    class Meta:
        verbose_name_plural = 'people'
# 不要这样写:
class Person(models.Model):
    first_name = models.CharField(max_length=20)
    last_name = models.CharField(max_length=40)
    class Meta:
        verbose_name_plural = 'people'
# 也不要这样写
class Person(models.Model):
    class Meta:
        verbose_name_plural = 'people'

    first_name = models.CharField(max_length=20)
    last_name = models.CharField(max_length=40)
  • If you define the __str__(former support for Python 3 is __unicode__), should be used in the data model class python_2_unicode_compatible()decorator.

  • Layout order within the data model class:

    1. Data item definitions
    2. Customized manager
    3. class Meta
    4. def __str__()
    5. def save()
    6. def get_absolute_url()
    7. Other customized approach
  • choices Should be defined as a tuple of tuples, the option name should be set to a single character class attribute, because it is the constant use of all-caps style, such as:

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

django.conf.settings use

Not access module top (part when the module is executed automatically import) django.conf.settings. Django project can only be called django.conf.settings.configure()function once (only once) for settings to be configured. settings is an LazyObjectobject that will really call it when access django.config.settings.configure()to settings for configuration.

If the top-level module it is accessed, it will automatically complete the django.conf.settings.configure()call, so the configuration settings after affects.

other

  • All strings should be an international mark
  • With the evolution of the code, not the removal of import, flake8 would be useless to importdisplay a warning message if you want to remove, add at the end of lines of code# NOQA
  • Removing the excess tail row whitespace
  • Do not write the name of the contributor in the code should be written in a separate AUTHORSfile

References: the Django Coding style

Guess you like

Origin www.cnblogs.com/haiiiiiyun/p/12558576.html