Python入門 Djangoを使う(その3)
はじめに
不定期連載のPython入門第12回目です。今回もこまごまとした構文学習をすっとばしてDjangoを使っていきます。
前々回で環境構築を行い、前回Webページを表示してみました。今回は前回作成したプロジェクトで作成されたファイルの内容を確認していきましょう。
今回の内容:
・プロジェクト生成時に自動生成されるファイルの確認。
–manage.py
–settings.py
manage.py
pythonスクリプトが記述されたファイルです。
様々なサブコマンドを持っており、プロジェクトの設定などはこのスクリプトを使って行います。
サブコマンドは、以下のようにmanage.pyを実行すると一覧表示されます。
$ ./manage.py
Available subcommands:
[auth]
changepassword
createsuperuser
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate //★
sendtestemail
shell
showmigrations
sqlflush
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject //★
test
testserver
[sessions]
clearsessions
[staticfiles]
collectstatic
findstatic
runserver //★
★マークはすでに実行したことのあるサブコマンドです。
今後幾つか使う予定ですので、詳しくはその時に。
settings.py
djangoの設定が記述されたファイルです。
項目をいくつかピックアップして説明します。
$ cat sample1/settings.py
"""
Django settings for sample1 project.
Generated by 'django-admin startproject' using Django 1.10.
For more information on this file, see
https://docs.djangoproject.com/en/1.10/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.10/ref/settings/
"""
import os //OSモジュールをインポートします。
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) //プロジェクトディレクトリの絶対パスを取得します。
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'xxxxxxxxxxx' //CSRF_TOKENなどセキュリティキーとして利用される定数SECRET_KEYを設定します。
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True //DEBUGモードをONにしています。
ALLOWED_HOSTS = [] //許可ユーザを指定します。
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'sample1.urls' //URLルーティングの設定を行うファイルの指定(デフォルト)
TEMPLATES = [ //テンプレートを指定します。
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'sample1.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
DATABASES = { //データベースを指定します。
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [ //認証のバリデーションの形式を指定します。
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/1.10/topics/i18n/
LANGUAGE_CODE = 'en-us' //言語指定
TIME_ZONE = 'UTC' //タイムゾーン指定
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/
STATIC_URL = '/static/' //静的ファイルの格納ディレクトリを指定します。
まとめ
今回はDjangoの軸となるmanage.pyというスクリプトと、設定ファイルsettings.pyを見てみました。
次回はプロジェクトディレクトリ以下のそれ以外のファイルを見てみましょう。