---
title: "設計思想"
version: 2.0
locale: ja
source: https://docs.djangoproject.com/ja/2.0/misc/design-philosophies/
canonical: https://djangodocs.dev/ja/2.0/misc/design-philosophies/
---
# 設計思想

このドキュメントでは、 Django の開発者たちがフレームワークの構築に取り入れ ている根本的な設計思想についていくつか解説します。それによって、 Django の これまでの経緯に説明を与えつつ、将来への指針にしたいと思います。

## 概要

### 疎結合

A fundamental goal of Django's stack is [loose coupling and tight cohesion](http://wiki.c2.com/?CouplingAndCohesion).
The various layers of the framework shouldn't "know" about each other unless
absolutely necessary.

For example, the template system knows nothing about Web requests, the database
layer knows nothing about data display and the view system doesn't care which
template system a programmer uses.

Although Django comes with a full stack for convenience, the pieces of the
stack are independent of another wherever possible.

### 短いコード

Django apps should use as little code as possible; they should lack boilerplate.
Django should take full advantage of Python's dynamic capabilities, such as
introspection.

### 素早い開発

The point of a Web framework in the 21st century is to make the tedious aspects
of Web development fast. Django should allow for incredibly quick Web
development.

### 同じことを繰り返さない (DRY)

Every distinct concept and/or piece of data should live in one, and only one,
place. Redundancy is bad. Normalization is good.

The framework, within reason, should deduce as much as possible from as little
as possible.

> **See also**
>
> The [discussion of DRY on the Portland Pattern Repository](http://wiki.c2.com/?DontRepeatYourself)

### 暗黙よりもはっきり示した方がいい (Explicit is better than implicit)

This is a core Python principle listed in [**PEP 20**](https://peps.python.org/pep-0020/), and it means Django
shouldn't do too much "magic." Magic shouldn't happen unless there's a really
good reason for it. Magic is worth using only if it creates a huge convenience
unattainable in other ways, and it isn't implemented in a way that confuses
developers who are trying to learn how to use the feature.

### 一貫性

フレームワークはあらゆるレベルで一貫性を持たなければなりません。一貫性は、低レベル (採用する Python のコーディングスタイル) から高レベル (Django の使い方) までのあらゆる点で保たれなければなりません。

## モデル

### 暗黙よりもはっきり示した方がいい (Explicit is better than implicit)

Fields shouldn't assume certain behaviors based solely on the name of the
field. This requires too much knowledge of the system and is prone to errors.
Instead, behaviors should be based on keyword arguments and, in some cases, on
the type of the field.

### Include all relevant domain logic

Models should encapsulate every aspect of an "object," following Martin
Fowler's [Active Record](https://www.martinfowler.com/eaaCatalog/activeRecord.html) design pattern.

This is why both the data represented by a model and information about
it (its human-readable name, options like default ordering, etc.) are
defined in the model class; all the information needed to understand a
given model should be stored *in* the model.

## データベース API

データベース API の主要な目的は、次のとおりです。

### SQL の効率性

It should execute SQL statements as few times as possible, and it should
optimize statements internally.

This is why developers need to call `save()` explicitly, rather than the
framework saving things behind the scenes silently.

This is also why the `select_related()` `QuerySet` method exists. It's an
optional performance booster for the common case of selecting "every related
object."

### Terse, powerful syntax

The database API should allow rich, expressive statements in as little syntax
as possible. It should not rely on importing other modules or helper objects.

Joins should be performed automatically, behind the scenes, when necessary.

Every object should be able to access every related object, systemwide. This
access should work both ways.

### Option to drop into raw SQL easily, when needed

The database API should realize it's a shortcut but not necessarily an
end-all-be-all. The framework should make it easy to write custom SQL -- entire
statements, or just custom `WHERE` clauses as custom parameters to API calls.

## URL デザイン

### 疎結合

URLs in a Django app should not be coupled to the underlying Python code. Tying
URLs to Python function names is a Bad And Ugly Thing.

Along these lines, the Django URL system should allow URLs for the same app to
be different in different contexts. For example, one site may put stories at
`/stories/`, while another may use `/news/`.

### Infinite flexibility

URLs should be as flexible as possible. Any conceivable URL design should be
allowed.

### ベストプラクティスの推奨

The framework should make it just as easy (or even easier) for a developer to
design pretty URLs than ugly ones.

File extensions in Web-page URLs should be avoided.

Vignette-style commas in URLs deserve severe punishment.

### Definitive URLs

Technically, `foo.com/bar` and `foo.com/bar/` are two different URLs, and
search-engine robots (and some Web traffic-analyzing tools) would treat them as
separate pages. Django should make an effort to "normalize" URLs so that
search-engine robots don't get confused.

This is the reasoning behind the [`APPEND_SLASH`](/ja/2.0/ref/settings/#std-setting-APPEND_SLASH) setting.

## テンプレートシステム

### ロジックのプレゼンテーションからの分離

私達は、テンプレートシステムはプレゼンテーションとプレゼンテーション関係のロジックを制御するためのツールであり、それ以上のものではないと考えています。 その本分をこえた機能をテンプレートシステムに求めるべきではありません。

### 冗長性の排除

大多数の動的な Web サイトでは、ヘッダやフッタ、ナビゲーションバーといった部分のデザインをサイト全体で共通にしています。 Django テンプレートシステムは、 こうしたサイトの構成要素を一箇所に保存しやすくし、重複したコードを削除する必要があります。

これが [テンプレートの継承](/ja/2.0/ref/templates/language/#template-inheritance) の背後にある思想です。

### HTML と疎結合であれ

テンプレートシステムは、ただ単に HTML だけを出力するように設計されてはいません。他のテキストベースのフォーマットや、単純なプレインテキストも同じように上手く生成できるように作られています。

### テンプレート言語として、XML を使うべきではない

テンプレートをパースするのに XML エンジンを使ってしまうと、テンプレートの編集に全く新しいヒューマンエラーの世界を作り出してしまい、結果として、テンプレート処理に受け入れられないようなレベルの困難を生み出してしまうでしょう。

### ページデザイナの有能さを前提にする

必ずしも Dreamweaver のような WYSIWYG エディタでうまく表示できるように テンプレートシステムを設計する必要はありません。そのような要求は制限が厳しすぎ、本来あるべきすっきりした構文を実現できなくなります。 Django では 直接 HTML を編集する作業に慣れたテンプレート作者を想定しています。

### ホワイトスペースを明示的に扱う

テンプレートシステムは、ホワイトスペースに対して変わった処理を行うべきではありません。もしテンプレートにホワイトスペースが書かれていれば、他の文字と同じように扱って、それをそのまま表示するべきです。テンプレートタグの外に書かれたすべてのホワイトスペースは、そのまま表示するべきです。

### 新しいプログラミング言語を開発しない

私たちの目的は、新しいプログラミング言語を開発することではありません。本当の目的は、プログラミング言語が持つような機能を必要十分なだけ使えるようにすることです。たとえば、条件分岐やループといったもので、これらはプレゼンテーションに関連する決定を下すときに大切になります。 [Django テンプレート言語 (Django Template Language; DTL)](/ja/2.0/topics/templates/#template-language-intro) は、複雑なロジックを使わずにこの目的を達成するために作られました。

Django のテンプレートシステムは、テンプレートの書き手は *プログラマー* ではなく、主に *デザイナー* であることを念頭に置いています。そのため、Python の知識がまったくなくても書けるようになっています。

### 安全性とセキュリティ

テンプレートシステムは、コマンドの実行やデータベースレコードの削除を行うような、悪意のあるコードの挿入ができないようになっていなければなりません。

これが、テンプレートシステムが任意の Python コードの実行を許さないようにできているもう一つの理由です。

### 拡張性

テンプレートシステムは、高度なテンプレート作者によるテクノロジの拡張に配慮せねばなりません。

これが、カスタムテンプレートタグとフィルタの背後にある思想です。

## ビュー

### シンプルであること

ビューは、Python の関数を書くのと同じくらい簡単に書けるべきです。開発者は、ふつうに関数を書くときと同じように、クラスからインスタンスを作ったりせずにビューが書けるべきです。

### request オブジェクトを使うこと

ビューはリクエストオブジェクトにアクセスします。リクエストオブジェクトとは、 現在のリクエストに関するメタデータを入れるオブジェクトです。ビューはこのオブジェクトをグローバル変数経由でアクセスするのではなく、引数として直接受け取るようにすべきです。それにより、「偽の」リクエストオブジェクトを渡してビューを簡単かつクリーンにテストできるようになります。

### 疎結合

ビューは、開発者がどのテンプレートシステムを使用しているか意識せずに使えるべきです。もっと言えば、テンプレートシステム自体を使っているかどうかも気にせずに使えるようにするべきです。

### GET と POST を区別

GET と POST は区別します。開発者は明示的に一方を他方と区別して明示的に使用するべきです。フレームワークは、GET と POST データを分かりやすく区別できるようにしなければなりません。

## キャッシュフレームワーク

Django の [キャッシュフレームワーク](/ja/2.0/topics/cache/) の主な目的は次の点にあります。

### 短いコード

キャッシュは可能な限り高速でなければならない。したがって、キャッシュバックエンドを包んでいるフレームワークのコード (特に `get()` 操作) はすべて、極限まで短くするべきである。

### 一貫性

キャッシュ API は、バックエントが違っていても一貫したインターフェイスを提供するべきである。

### 拡張性

キャッシュ API は、開発者のニーズにもとづいて、アプリケーションレベルで拡張可能であるべきである (例としては [Cache key transformation](/ja/2.0/topics/cache/#cache-key-transformation) がある)。
