Two Scoop of Django 笔记

核心理念 Core Concepts

  • keep It Simple, Stupid
    Simplelicity is the ultimate sophistication
  • Fat Models, Utility Modules, Thin Views, Stupid Templates
  • Start With Django By Default
  • Be Familiar with Django's Design Plilosophies
  • The Twelve-Factor App

1.编码风格 Coding Style

The Word on Imports

  1. Standard library imports
  2. Related third-party imports
  3. Local application or library specify imports

In Django project:

  1. Standard library imports
  2. Imports from core Django
  3. Imports from third-party apps including those unrelated th Django
  4. Imports from the apps that you created as part of your Django project.

Use Explictit Relative Imports

bad imports include hardcoded import

# cones/views.py
from django.views.generic import CreateView

from cones.models import WaffleCone
from cones.forms import WaffleConeForm
from core.views import FoodMixin


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

Sure, your cones app works nice within your ice cream tracker project, but it has those nasty hardcoded
imports that make it less portable and reusable:

  • What if you wanted to reuse your cones app in another project that tracks your general dessert
    consumption, but you had to change the name due to a naming confict (e.g. a confict with a
    Django app for snow cones)?
  • What if you simply wanted to change the name of the app at some point?

在两种情况下硬编码的导入会变得不方便和不可复用:

  • 需要在其他项目使用cones app但是因为命名冲突需要改名
  • 某些情况下你想改变app的名字

correct example

# cones/views.py

from django.views.generic import CreateView

from .models import WaffleCone
from .forms import WaffleConeForm
from core.views import FoodMixin


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

2. The Optimal Django Environment Setup

Use the Same Database Engine Everywhere

virtualenvwrapper is better than virtualenv

3. How to Lay Out Django Project

<repository_root>/
    <django_project_root>/
        <configuration_root>/

4. Fundamentals of Django App Design

The Golden Rule of Django App Design

James Bennett volunteers as both a Django core developer and as its release manager. He taught us
everything that we know about good Django app design. We quote him:

“ The art of creating and maintaining a good Django app is that it should follow the
truncated Unix philosophy according to Douglas McIlroy: ‘Write programs that do one
thing and do it well.”’

In essence, each app should be tightly focused on its task. If an app can’t be explained in a single
sentence of moderate length, or you need to say ‘and’ more than once, it probably means the app is
too big and should be broken up.

When in Doubt, Keep Apps Small

Don’t worry too hard about getting app design perfect. It’s an art, not a science. Sometimes you have
to rewrite them or break them up. at’s okay.
Try and keep your apps small. Remember, it’s better to have many small apps than to have a few
giant apps.

Uncommon App Modules

scoops/
    behaviors.py
    constants.py
    context_processors.py
    decorators.py
    db/
    exceptions
    fields.py
    factories.py
    helpers.py
    managers.py
    middleware.py
    signals.py
    utils.py
    viewmixins.py  

behaviors.py : An option for locating model mixins per subsection.
constants.py : A good name for placement of app-level settings. If there are enough of them involved
in an app, breaking them out into their own module can add clarity to a project.
decorators.py Where we like to locate our decorators. For more information on decorators.
db/ Used in many projects for any custom model elds or components.
fields.py is commonly used for form elds, but is sometimes used for model elds when there isn’t
enough eld code to justify creating a db/ package.
factories.py Where we like to place our test data factories. Described in brief in subsection.
helpers.py What we call helper functions. ese are where we put code extracted from views and models to make them lighter. Synonymous with utils.py
managers.py When models.py grows too large, a common remedy is to move any custom model
managers to this module.
signals.py While we argue against providing custom signals (see chapter 28), this can be a useful
place to put them.
utils.py Synonymous with helpers.py
viewmixins.py View modules and packages can be thinned by moving any view mixins to this mod-
ule.

5.Settings and Requirements Files

Avoid Non-Versioned Local Settings 避免无版本的本地设置

We used to advocate the non-versioned local settings anti-pattern. Now we know better.

As developers, we have our own necessary settings for development, such as settings for debug tools
which should be disabled (and often not installed to) staging or production servers.

Furthermore, there are often good reasons to keep specifc settings out of public or private code
repositories. The SECRET_KEY setting is the first thing that comes to mind, but API key settings to
services like Amazon, Stripe, and other password-type variables need to be protected.

A common solution is to create local settings.py modules that are created locally per server or develop-
ment machine, and are purposefully kept out of version control. Developers now make development-
specific settings changes, including the incorporation of business logic without the code being tracked
in version control. Staging and deployment servers can have location specifc settings and logic with-
out them being tracked in version control.

What could possibly go wrong?!?

Ahem...

  • Every machine has untracked code.
  • How much hair will you pull out, when after hours of failing to duplicate a production bug
    locally, you discover that the problem was custom logic in a production-only setting?
  • How fast will you run from everyone when the ‘bug’ you discovered locally, fixed and pushed
    to production was actually caused by customizations you made in your own local settings.py
    module and is now crashing the site?
  • Everyone copy/pastes the same local settings.py module everywhere. Isn’t this a violation of
    Don’t Repeat Yourself but on a larger scale?

我们曾经提倡无版本的反模式本地设置,现在我们了解的更多。
作为开发者, 我们各自拥有针对开发环境的重要设置, 比如为了解决问题工具的设置, 但这个在生产服务中禁用。
长远的看,让特殊的设定远离公共或者私人的仓库有很多理由。SECRET_KEY的设置是第一个需要保护的,不仅如此, 其他服务的API设置或者是需要密码保护的变量同样需要保护。
一个通用的设置是为每个服务或者开发机器创建一个本地的setting.py,并且让它们脱离版本控制。开发者做了为开发环境制定的设定改变,包括没有在版本控制中追踪的事务逻辑合并。状态和开发服务拥有本地的特别设定和脱离版本控制的逻辑。
会不会出现问题呢?
然而。。。

  • 每一台服务器都有有无法追踪的代码
  • 每当你失败地复制一个生产环境问题到本地,你发现开发环境的问题存在于一个特定的本地逻辑,这花掉的时间要掉多少头发呢?
  • 由于你的个人本地设置模块放在每一个环境,网站奔溃了, 你需要多快才能在每一个环境里发现本地的设置问题, 修复并且推送到开发环境呢。
  • 每个人到处复制粘贴同样一个settings.py, 在打的方向上是不是和不要重复你自己冲突了?
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,204评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,091评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,548评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,657评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,689评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,554评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,302评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,216评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,661评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,851评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,977评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,697评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,306评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,898评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,019评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,138评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,927评论 2 355

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,332评论 0 10
  • 盛夏风临狄道中,惊天滂雨洗旧尘。 昔岁初至省府地,一袭灰霾惆怅人。 金城飘飘红旗舞,波澜壮志更星辰。 浮萍终随流水...
    牛谷河畔阅读 192评论 0 0
  • 自我解梦案例分析 梦例「生物课」 梦者:二丁目 梦已经很模糊了,大概是我在高中课堂上,老师讲的是生物课,然后让我回...
    二丁目先生阅读 703评论 0 4
  • 千里云上行,白云复苍狗。 古之人眼里,云是神一样的存在。 最壮阔的我以为是《逍遥游》:鹏之背,不知其几千里也;怒而...
    听风66699阅读 401评论 0 1
  • 田间阡陌上的花开了,你可以一边赏花,一边慢慢地回来,而我可以慢慢等你回来。 都说"最是无情帝王家”,而钱镠...
    momo_me阅读 337评论 0 0