はじめに
Djangoのプロジェクトを作成した際に、いつも(自分が)実施している「定型作業」をまとめたものです。
(備忘録的に作成していますので、あくまで参考程度にご覧ください。)
プロジェクトの作成
$ mkdir hogehoge
$ cd hogehoge
$ django-admin startproject config . ←最後のピリオドを忘れないこと
以下の様なフォルダ構成になる。
hogehoge
├── config
│ ├── __init__.py
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
config以下にプロジェクトの設定ファイルが格納される。
setting.pyの修正
タイムゾーン及び言語コードを修正する。
config/setting.py
LANGUAGE_CODE = 'ja'
TIME_ZONE = 'Asia/Tokyo'
ここでmigrateしておく。
$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying sessions.0001_initial... OK
ここでサーバーを起動して、ブラウザから
http://127.0.0.1:8000/
にアクセスして日本語の画面になっていることを確認する。

templateファイルを格納するディレクトリを用意する
複数のアプリを開発する時、ファイル名が被ると面倒なのでアプリ単位で
格納先を分けて管理する(以下の様なイメージ)。
── templates
├── app1
│ ├── create.html
│ ├── index.html
│ └── update.html
├── app2
│ ├── index.html
│ └── update.html
└── base.html
コンソールからtemplatesディレクトリを作成し、
$ mkdir templates
setting.pyに格納先を反映する。
config/setting.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')], # この行を修正する
'APP_DIRS': True,
staticファイル格納ディレクトリを用意する
templateファイルと同様に、staticファイルもアプリごとに格納できる様にディレクトリを分ける。
以下の様なイメージ
├── static
│ ├── css
│ │ ├── app1
│ │ └── app2
│ └── images
│ ├── app1
│ └── app2
コンソールからstaticディレクトリを作成する。
$ mkdir static
setting.pyに格納先を反映する。
config/setting.py
PROJECT_NAME = os.path.basename(BASE_DIR) #この行を追加
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')] #この行を追加
STATIC_ROOT = '/var/www/{}/static'.format(PROJECT_NAME) #この行を追加
STATIC_URL = '/static/'