環境構築

DjangoのWebアプリ作成(local環境編)

こんにちは、ともです。

今回はDjangoのWebアプリの作成(local環境構築)について投稿します。

手順

手順は以下の通り行います。



webアプリ作成

webアプリ作成を作成します。アプリを作成するディレクトリに移動します。

    
/data
 ┗ /work
      ┗  /docker
             ┗  /docker_www
                   ┣ db.sqlite3
                   ┣ docker-compose.yml
                   ┣ Dockerfile
                   ┣ manage.py
                   ┣ requirements.txt
                   ┗ website
                             ┣ __init__.py
                             ┣ settings.py
                             ┣ urls.py
                             ┗ wsgi.py

    
  

docker_www配下で以下のコマンドを入力します。

  
sudo docker-compose run --rm web django-admin startapp mrrs
  

以下のディレクトリ構成になります。

    
/data
 ┗ /work
      ┗  /docker
             ┗  /docker_www
                   ┣ db.sqlite3
                   ┣ docker-compose.yml
                   ┣ Dockerfile
                   ┣ manage.py
                   ┣ requirements.txt
                   ┣ website
                   ┃   ┣ __init__.py
                   ┃   ┣ settings.py
                   ┃   ┣ urls.py
                   ┃   ┗ wsgi.py
                   ┗ mrrs
                        ┣ admin.py
                        ┣ apps.py
                        ┣ __init__.py
                        ┣ models.py
                        ┣ tests.py
                        ┣ views.py
                        ┣ urls.py
                        ┣ forms.py
                        ┗ migrations
                              ┗ __init__.py
    
  

再起動などした場合コンテナが起動していない場合があります。以下のコマンドを入力します

sudo systemctl start docker

会議室予約システムのプログラミングについては省略します。

データベース設定

docker_www/website/setting.pyをローカル環境用に作成します。

    
"""
Django settings for website project.

Generated by 'django-admin startproject' using Django 2.2.

For more information on this file, see
https://docs.djangoproject.com/en/2.2/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.2/ref/settings/
"""

import 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/2.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
#SECRET_KEY = 'hy0)+%$x_4_(_r4vmq@z1#s6$5%__+!v=mqkr@_31h)%nfm5g2'
SECRET_KEY = os.environ.get('SECRET_KEY', default='foo')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
#DEBUG = int(os.environ.get('DEBUG', default=0))

#ALLOWED_HOSTS = ['*']
#ALLOWED_HOSTS = ['localhost', '127.0.0.1']

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'mrrs.apps.MrrsConfig',
]

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 = 'website.urls'

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',
            ],
        },
    },
]

WSGI_APPLICATION = 'website.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}
ALLOWED_HOSTS = []

# Password validation
# https://docs.djangoproject.com/en/2.2/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/2.2/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/2.2/howto/static-files/

STATIC_URL = '/static/'
    
  

docker-composeを開発用にdocker-compose-dev.ymlとして作成します。

    
version: "3"

services:
  web:
    build: .
    volumes:
      - .:/code
    environment:
      - DJANGO_SETTINGS_MODULE=website.settings_development
    ports:
      - 8000:8000
    command: python manage.py runserver 0.0.0.0:8000

    
  

テーブル(create文)を作成します。

  
sudo docker-compose -f docker-compose-dev.yml run web python manage.py  makemigrations
  

テーブルを作成します。

  
sudo docker-compose -f docker-compose-dev.yml run web python manage.py migrate
  

テーブル状態を確認します。

  
sudo docker-compose -f docker-compose-dev.yml run web python manage.py showmigrations
  

実行

実行します。

  
sudo docker-compose -f docker-compose-dev.yml up
  

まとめ

今回はは開発環境にDjangoのアプリ作成と、setting.pyおよび、docker-compose-dev.ymlの作成について記載しました。

次回はHerokuについて記載したいと思います。

コメント

0 件のコメント:

コメントを投稿

コメントをお待ちしています。