Python & Flask & Django

Django Coding Style

weicome 2017. 4. 6. 21:47





Python Style


- 특별한 예외상황이 아니라면 Python Style Guide 명세인 PEP8 을 따른다.


- 들여쓰기 할 때는 4칸을 들여쓴다.


- 함수/변수명 에는 camelCase가 아닌 usercore(_)를 사용한다

ex) get_unique_voters() (O) /  getUniqueVoters (X)


- 주석은 아래와 같이 사용한다


def foo():

    """

    Calculate something and return the result.

    """

    ...






Import


- import의 경우 내장 라이브러리, third-party 라이브러리, 장고 컴포턴트, 로컬 장고 컴포넌트 순으로 작성한다. 


- 각 라인은 알파벳 순으로 정렬한다.


#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,

)


# local Django


from .models import LogEntry




Template Style


장고 템플릿 코드에서 중괄호 사이에 한칸씩 띄어서서 작성한다.


바른 예

{{ foo }}


잘못된 예

{{foo}}




Model Style 


필드의 이름은 반드시 소문자를 사용하며 camelCase 가 아닌 userscore(_)를 사용한다.


바른 예

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)