目标:执行python manage.py runserver
时自动进行代码检查。
要达成该目标,可借助Django内置signal:autoreload_started
。
安装以下packages:
1
2
3
4
5
6
7
8
9
10
11
12flake8
flake8-django
flake8-assertive
flake8-return
flake8-tabs
flake8-print
flake8-colors
flake8-html
flake8-noqa
flake8-literal
flake8-todos
flake8-raise然后修改
manage.py
文件,添加check_style
函数,内容如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
def check_style(*args, **kwargs):
try:
from flake8.main.cli import main as flake8
flake8([
"--extend-ignore", "LIT101,DJ08",
"--max-line-length", "120",
"--max-doc-length", "150",
"--tee",
"--show-source",
"--max-complexity", "10",
"--select", "E,F,W,C90",
"--extend-exclude", "manage.py,*/admin.py,*/migrations/*,*/tests*",
"--format", ":".join([
"${cyan}%(path)s${reset}:${yellow_bold}%(row)d${reset}:${green_bold}%(col)d${reset}",
"${red_bold}%(code)s${reset} %(text)s"
]),
])
except (ImportError, SystemExit):
pass