LoginSignup
0
1

More than 3 years have passed since last update.

【メモ】Djnago開発環境

Posted at

pyenvを入れる

$ git clone https://github.com/yyuu/pyenv.git ~/.pyenv
$ export PYENV_ROOT="$HOME/.pyenv"
$ export PATH="$PYENV_ROOT/bin:$PATH"
$ eval "$(pyenv init -)"

最新のpythonを入れる

最新のpythonを調べる

$ pyenv install --list

最新のpythonをインストール

$ pyenv install 3.8.1
$ pyenv local 3.8.1
$ python -V
Python 3.8.1

pipenvを入れる

$ pip install pipenv

pipenvでインストールすると通常、~/.local/share/virtualenvs/に入るが、プロジェクト直下の.venvに入るようにする。.venvは仮想環境に入ったときに自動的に作成される。

$ echo 'export PIPENV_VENV_IN_PROJECT=1' >> ~/.bash_profile

pipenvの仮想環境内で使用するpythonのバージョンを指定する

$ pipenv --python 3.8

仮想環境に入る。

$ pipenv shell
django
│── Pipfile
│── Pipfile.lock
└── .venv

パッケージ管理

$ pipenv install django django-bootstrap4
$ pipenv install --dev django-debug-toolbar django-webpack-loader #開発環境のみ

pipenvはデフォルトではプレリリース版のパッケージをインストールすることはできません。以下のようにPipfileに設定を追加する必要があります。

Pipfile
[pipenv]
allow_prereleases = true

Djangoプロジェクト

pipenvの仮想環境内でプロジェクトを作成

$ django-admin startproject config .
django
│── config
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
│── Pipfile
│── Pipfile.lock
├── .venv
└── manage.py

セッティングファイルを分割

django
├── config
│   ├── __init__.py
│   ├── settings
│   │   ├── __init__.py
│   │   ├── base.py # 共通の設定
│   │   ├── production.py # 本番環境だけに適用したい設定
│   │   ├── development.py # 開発環境だけに適用したい設定
│   │   └── test.py # テストだけに適用したい設定
│   ├── urls.py
│   └── wsgi.py
│── Pipfile
│── Pipfile.lock
├── .venv
└── manage.py

ディレクトリが変わったので、base.pyのBASE_DIRを修正

base.py
- BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+ BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
production.py
# 本番環境だけに適用したい設定
from .base import *

DEBUG = True
development.py
# 開発環境だけに適用したい設定
from .base import *

DEBUG = False

アプリケーション作成

python manage.py startapp test_app 
django
├── config
│── Pipfile
│── Pipfile.lock
├── .venv
│── manage.py
└── test_app
    ├── __init__.py
    ├── admin.py
    ├── apps.py
    ├── migrations
    ├── models.py
    ├── tests.py
    └── views.py

テンプレートをまとめる

base.py
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        + 'DIRS': [os.path.join(BASE_DIR, "templates")],
        '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',
            ],
        },
    },
]
django
├── config
│── Pipfile
│── Pipfile.lock
├── .venv
│── manage.py
│── templates
│   └── test_app
└── test_app
    ├── __init__.py
    ├── admin.py
    ├── apps.py
    ├── migrations
    ├── models.py
    ├── tests.py
    └── views.py

staticをまとめる

アプリケーションをまとめる

参考

https://fclef.jp/20191103/
https://studygyaan.com/django/best-practice-to-structure-django-project-directories-and-files

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1