two-scoops-of-django

django学习书籍two scoop of django的心得整理

导入包时要用相对路径导入

不要这样做

1
2
3
4
5
6
7
8
9
10
# DON'T DO THIS!
# Hardcoding of the 'cones' package
# with implicit relative imports
from cones.models import WaffleCone
from cones.forms import WaffleConeForm
from core.views import FoodMixin

class WaffleConeCreateView(FoodMixin, CreateView):
model = WaffleCone
form_class = WaffleConeForm

应该这样做

1
2
3
4
5
6
7
8
9
10
11
# cones/views.py
from django.views.generic import CreateView

# Relative imports of the 'cones' package
from .models import WaffleCone
from .forms import WaffleConeForm
from core.views import FoodMixin

class WaffleConeCreateView(FoodMixin, CreateView):
model = WaffleCone
form_class = WaffleConeForm

包导入时注意顺序

1
2
3
4
5
6
7
8
9
10
11
# cones/views.py
from django.views.generic import CreateView

# Relative imports of the 'cones' package
from .models import WaffleCone
from .forms import WaffleConeForm
from core.views import FoodMixin

class WaffleConeCreateView(FoodMixin, CreateView):
model = WaffleCone
form_class = WaffleConeForm

用Cookiecutter生成django项目

命令行

1
cookiecutter https://github.com/pydanny/cookiecutter-django

使用多个设置文件

1
2
3
4
5
6
7
8

settings/
├── __init__.py
├── base.py
├── local.py
├── staging.py
├── test.py
├── production.py

1

然后如果要运行时的时候指定某个settings文件

1
python manage.py shell --settings=twoscoops.settings.local

使用抽象类继承类