---
title: "Djangoの設定"
version: 3.1
locale: ja
source: https://docs.djangoproject.com/ja/3.1/topics/settings/
canonical: https://djangodocs.dev/ja/3.1/topics/settings/
---
# Djangoの設定

Djangoの設定ファイルには、インストールしたDjangoの全ての設定が含まれています。このドキュメントでは、各設定の役割と、どのような設定を利用できるか説明していきます。

## 基礎

設定ファイルはモジュールレベルの変数が記述された、ただのPythonモジュールです。

設定例を幾つか示します:

```
ALLOWED_HOSTS = ['www.example.com']
DEBUG = False
DEFAULT_FROM_EMAIL = 'webmaster@example.com'
```

> **Note**
>
> もし [`DEBUG`](/ja/3.1/ref/settings/#std-setting-DEBUG) を `False` にした場合、 [`ALLOWED_HOSTS`](/ja/3.1/ref/settings/#std-setting-ALLOWED_HOSTS) も設定する必要があります。

設定ファイルはPythonのモジュールなので、以下のような性質を備えています。

- Pythonの構文エラーになってはいけません。
- 通常の Pythonの構文を使って、動的に値を設定する事ができます。例えば:

  ```
  MY_SETTING = [str(i) for i in range(30)]
  ```
- 他の設定ファイルから値を import できます。

## 設定ファイルの指定

#### `DJANGO_SETTINGS_MODULE`

When you use Django, you have to tell it which settings you're using. Do this
by using an environment variable, [`DJANGO_SETTINGS_MODULE`](#envvar-DJANGO_SETTINGS_MODULE).

The value of [`DJANGO_SETTINGS_MODULE`](#envvar-DJANGO_SETTINGS_MODULE) should be in Python path syntax,
e.g. `mysite.settings`. Note that the settings module should be on the
Python [import search path](https://www.diveinto.org/python3/your-first-python-program.html#importsearchpath).

### `django-admin` ユーティリティ

[django-admin](/ja/3.1/ref/django-admin/) を使う場合、環境変数を予め指定しておくか、ユーティリティを起動する度に設定モジュールを明示的に渡します。

例 (Unix Bash シェル):

```
export DJANGO_SETTINGS_MODULE=mysite.settings
django-admin runserver
```

例(Windows コマンドプロンプト):

```
set DJANGO_SETTINGS_MODULE=mysite.settings
django-admin runserver
```

コマンドライン引数で設定モジュールを指定するには、 --settings\` を使用します:

```
django-admin runserver --settings=mysite.settings
```

### サーバ (`mod_wsgi`) の設定

実際のサーバ環境では、 WSGI アプリケーションにどの設定ファイルを使うのか教えてあげる必要があります。これには `os.environ`:: を使います。

```
import os

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
```

さらなる情報や他のDjango WSGI アプリケーションでの共通の設定は [Django mod\_wsgi ドキュメント](/ja/3.1/howto/deployment/wsgi/modwsgi/) を参照してください。

## デフォルトの設定

Django の設定ファイルでは、特に必要のない限り設定をおこなう必要はありません。 各々の設定には注意深く決められたデフォルト値が入っています。デフォルト値は `django/conf/global_settings.py` に記述されています。

Django が設定をコンパイルする際には、以下のようなアルゴリズムを使います:

- `global_settings.py` から設定を読みだす。
- 指定された設定ファイルから設定を読み込み、グローバルな設定を必要に応じて上書きします。

設定ファイルから `global_settings` を読み込むのは冗長なのでやらないでください。

### 設定の変更を確かめる

The command `python manage.py diffsettings` displays differences between the
current settings file and Django's default settings.

詳しくは [`diffsettings`](/ja/3.1/ref/django-admin/#django-admin-diffsettings) のドキュメントを参照してください。

## Pythonのコード内で設定を参照する

自作のDjangoアプリケーションからは `django.conf.settings` をimportすることで設定を参照できます。例:

```
from django.conf import settings

if settings.DEBUG:
    # Do something
```

`django.conf.settings` はモジュールではなく、ただのオブジェクトです。そのため個々の設定は別々にimportできません。

```
from django.conf.settings import DEBUG  # This won't work.
```

また `global_settings` や自作の設定ファイルをimportしてはいけません。 `django.conf.settings` はデフォルト設定とサイト固有の設定を抽象化し、単一のインタフェースを提供しています。また設定を使うコードと設定ファイルの場所を分離しています。

## 実行時に設定を変更する

アプリケーションの実行中に設定を変えてはいけません。例えば、viewの中で以下の実装を行ってはいけません。

```
from django.conf import settings

settings.DEBUG = True   # Don't do this!
```

設定を変えて良いのは、設定ファイルの中だけです。

## セキュリティ

設定ファイルにはデータベースのパスワードのような重要な情報が入っているため、設定ファイルへのアクセスは出来るだけ制限してください。例えば、設定ファイルのパーミッションを、あなたとWebサーバだけが読み取りできるようにしてください。共有ホスティング環境で運用する場合、これは極めて重要な事項です。

## 利用可能な設定

利用可能な設定は [settings リファレンス](/ja/3.1/ref/settings/) を参照してください。

## 設定項目を自作する

There's nothing stopping you from creating your own settings, for your own
Django apps, but follow these guidelines:

- Setting names must be all uppercase.
- 既存の設定の再発明はやめましょう。

設定値をシーケンスにする際は、リストを使ってください。ただしこれはDjangoの慣習です。

## Using settings without setting [`DJANGO_SETTINGS_MODULE`](#envvar-DJANGO_SETTINGS_MODULE)

In some cases, you might want to bypass the [`DJANGO_SETTINGS_MODULE`](#envvar-DJANGO_SETTINGS_MODULE)
environment variable. For example, if you're using the template system by
itself, you likely don't want to have to set up an environment variable
pointing to a settings module.

こういったケースのため、Djangoでは以下の関数を呼ぶことで手動で設定できるようになっています。

#### `django.conf.settings.configure(default_settings, **settings)`

実装例:

```
from django.conf import settings

settings.configure(DEBUG=True)
```

Pass `configure()` as many keyword arguments as you'd like, with each keyword
argument representing a setting and its value. Each argument name should be all
uppercase, with the same name as the settings described above. If a particular
setting is not passed to `configure()` and is needed at some later point,
Django will use the default setting value.

Configuring Django in this fashion is mostly necessary -- and, indeed,
recommended -- when you're using a piece of the framework inside a larger
application.

したがって、`settings.configure()` を介して設定された場合、Djangoはプロセス環境変数を変更しません (これが通常発生する理由については、[`TIME_ZONE`](/ja/3.1/ref/settings/#std-setting-TIME_ZONE) のドキュメントを参照してください)。これらの場合、すでに環境を完全に制御していることが前提となります。

### Custom default settings

If you'd like default values to come from somewhere other than
`django.conf.global_settings`, you can pass in a module or class that
provides the default settings as the `default_settings` argument (or as the
first positional argument) in the call to `configure()`.

In this example, default settings are taken from `myapp_defaults`, and the
[`DEBUG`](/ja/3.1/ref/settings/#std-setting-DEBUG) setting is set to `True`, regardless of its value in
`myapp_defaults`:

```
from django.conf import settings
from myapp import myapp_defaults

settings.configure(default_settings=myapp_defaults, DEBUG=True)
```

The following example, which uses `myapp_defaults` as a positional argument,
is equivalent:

```
settings.configure(myapp_defaults, DEBUG=True)
```

Normally, you will not need to override the defaults in this fashion. The
Django defaults are sufficiently tame that you can safely use them. Be aware
that if you do pass in a new default module, it entirely *replaces* the Django
defaults, so you must specify a value for every possible setting that might be
used in that code you are importing. Check in
`django.conf.settings.global_settings` for the full list.

### Either `configure()` or [`DJANGO_SETTINGS_MODULE`](#envvar-DJANGO_SETTINGS_MODULE) is required

If you're not setting the [`DJANGO_SETTINGS_MODULE`](#envvar-DJANGO_SETTINGS_MODULE) environment
variable, you *must* call `configure()` at some point before using any code
that reads settings.

If you don't set [`DJANGO_SETTINGS_MODULE`](#envvar-DJANGO_SETTINGS_MODULE) and don't call
`configure()`, Django will raise an `ImportError` exception the first time
a setting is accessed.

If you set [`DJANGO_SETTINGS_MODULE`](#envvar-DJANGO_SETTINGS_MODULE), access settings values somehow,
*then* call `configure()`, Django will raise a `RuntimeError` indicating
that settings have already been configured. There is a property for this
purpose:

例:

```
from django.conf import settings
if not settings.configured:
    settings.configure(myapp_defaults, DEBUG=True)
```

Also, it's an error to call `configure()` more than once, or to call
`configure()` after any setting has been accessed.

It boils down to this: Use exactly one of either `configure()` or
[`DJANGO_SETTINGS_MODULE`](#envvar-DJANGO_SETTINGS_MODULE). Not both, and not neither.

### Calling `django.setup()` is required for "standalone" Django usage

If you're using components of Django "standalone" -- for example, writing a
Python script which loads some Django templates and renders them, or uses the
ORM to fetch some data -- there's one more step you'll need in addition to
configuring settings.

After you've either set [`DJANGO_SETTINGS_MODULE`](#envvar-DJANGO_SETTINGS_MODULE) or called
`configure()`, you'll need to call [`django.setup()`](/ja/3.1/ref/applications/#django.setup) to load your
settings and populate Django's application registry. For example:

```
import django
from django.conf import settings
from myapp import myapp_defaults

settings.configure(default_settings=myapp_defaults, DEBUG=True)
django.setup()

# Now this script or any imported module can use any part of Django it needs.
from myapp import models
```

Note that calling `django.setup()` is only necessary if your code is truly
standalone. When invoked by your Web server, or through [django-admin](/ja/3.1/ref/django-admin/), Django will handle this for you.

> **django.setup() may only be called once.**
>
> Therefore, avoid putting reusable application logic in standalone scripts
> so that you have to import from the script elsewhere in your application.
> If you can't avoid that, put the call to `django.setup()` inside an
> `if` block:
>
> ```
> if __name__ == '__main__':
>     import django
>     django.setup()
> ```

> **See also**
>
> **[The Settings Reference](/ja/3.1/ref/settings/)**
>
>   Contains the complete list of core and contrib app settings.
