TIL39. Django : Project Settings

ID์งฑ์žฌยท2021๋…„ 10์›” 15์ผ
2

Django

๋ชฉ๋ก ๋ณด๊ธฐ
32/43
post-thumbnail

๐Ÿ“Œ ์ด ํฌ์ŠคํŒ…์—์„œ๋Š” Django ํ”„๋กœ์ ํŠธ๋ฅผ ์œ„ํ•œ ์ดˆ๊ธฐ ์„ธํŒ…์— ๋Œ€ํ•ด ์ •๋ฆฌํ•˜์˜€์Šต๋‹ˆ๋‹ค.



๐ŸŒˆ Project Settings

๐Ÿ”ฅ ๊ฐ€์ƒํ™˜๊ฒฝ ์ƒ์„ฑ ๋ฐ ํ”„๋กœ์ ํŠธ ๊ตฌ์„ฑ

๐Ÿ”ฅ Django์™€ MySQL ์—ฐ๊ฒฐ ์ค€๋น„

๐Ÿ”ฅ settgins.py ์„ค์ •

๐Ÿ”ฅ git & .gitignore ์„ค์ •



Step1. ๊ฐ€์ƒํ™˜๊ฒฝ ์ƒ์„ฑ ๋ฐ ํ”„๋กœ์ ํŠธ ๊ตฌ์„ฑ

๐Ÿค” ๊ฐ€์ƒํ™˜๊ฒฝ ์ƒ์„ฑ

โœ”๏ธ item2์—์„œ ํ”„๋กœ์ ํŠธ๋ฅผ ์œ„ํ•œ ์ƒˆ๋กœ์šด ๊ฐ€์ƒํ™˜๊ฒฝ์„ ์ƒ์„ฑํ•˜๊ณ , ํ™œ์„ฑํ™” ์‹œํ‚จ๋‹ค.

$ conda create -n [๊ฐ€์ƒํ™˜๊ฒฝ๋ช…] python=3.8 ๐Ÿ‘ˆ ๊ฐ€์ƒํ™˜๊ฒฝ ์ƒ์„ฑ

$ conda env list ๐Ÿ‘ˆ ์ƒ์„ฑํ•œ ๊ฐ€์ƒํ™˜๊ฒฝ ๋ฆฌ์ŠคํŠธ ํ™•์ธ

$ conda activate [๊ฐ€์ƒํ™˜๊ฒฝ๋ช…] ๐Ÿ‘ˆ ๊ฐ€์ƒํ™˜๊ฒฝ ํ™œ์„ฑํ™”

๐Ÿค” ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

โœ”๏ธ ๊ฐ€์ƒํ™˜๊ฒฝ์ด ํ™œ์„ฑํ™”๋œ ์ƒํƒœ์—์„œ Django๋ฅผ ์„ค์น˜ํ•˜๊ณ , Project๋ฅผ ๋งŒ๋“ ๋‹ค.

$ pip install django ๐Ÿ‘ˆ django ์ตœ์‹  ๋ฒ„์ „ ์„ค์น˜

$ pip freeze ๐Ÿ‘ˆ ์„ค์น˜๋œ ํ”„๋กœ๊ทธ๋žจ ํ™•์ธ

$ django-admin startproject [ํ”„๋กœ์ ํŠธ๋ช…] ๐Ÿ‘ˆ django ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ



2. Django์™€ MySQL ์—ฐ๊ฒฐ ์ค€๋น„

๐Ÿค” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ƒ์„ฑ ๋ฐ mysqlclient ์„ค์น˜

โœ”๏ธ ๊ฐ€์ƒํ™˜๊ฒฝ์ด ํ™œ์„ฑํ™”๋œ ์ƒํƒœ์—์„œ mysqlclient๋ฅผ ์„ค์น˜ํ•ด์ค€๋‹ค.

$ pip install mysqlclient ๐Ÿ‘ˆ django ์ตœ์‹  ๋ฒ„์ „ ์„ค์น˜

โœ”๏ธ mysql์ด ์„ค์น˜๋œ ์ƒํƒœ์—์„œ mysql๋กœ ์ง„์ž…ํ•ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์ƒ์„ฑํ•ด ์ค€๋‹ค. ์ด๋Š” ์ถ”ํ›„ django settings.py์—์„œ sqllite3๋ฅผ ๋Œ€์ฒดํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค.

$ mysql -u root -p ๐Ÿ‘ˆ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด root ์‚ฌ์šฉ์ž๋กœ mysql ์ง„์ž…

mysql> create database [DB์ด๋ฆ„] character set utf8mb4 collate utf8mb4_general_ci; ๐Ÿ‘ˆ DB ์ƒ์„ฑ

mysql> show databases ๐Ÿ‘ˆ DB ์ƒ์„ฑ ํ™•์ธ

โœ”๏ธ root์‚ฌ์šฉ์ž๋กœ mysql์— ์ง„์ž…ํ•œ ์ƒํƒœ์—์„œ ์›ํ•œ๋‹ค๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY '[๋ณ€๊ฒฝํ•  ๋น„๋ฐ€๋ฒˆํ˜ธ]'; ๐Ÿ‘ˆ ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ณ€๊ฒฝ



3. Django settgins.py

๐Ÿค” IP ํ—ˆ์šฉ(ํ™”์ดํŠธ๋ฆฌ์ŠคํŠธ ๊ธฐ๋ฐ˜)

โœ”๏ธ ์—ฌ๊ธฐ๊นŒ์ง€ ์™„๋ฃŒํ–ˆ๋‹ค๋ฉด, Django settgins์—์„œ ๋ช‡๊ฐ€์ง€ settgins์„ ๋‹ค๋ค„๋ณด์ž.

ALLOWED_HOSTS = ['*'] ๐Ÿ‘ˆ IP ์ „๋ถ€ ํ—ˆ์šฉ

๐Ÿค” SECRET_KEY ๋ฐ DATABASES ์€ํ์‹œํ‚ค๊ธฐ

โœ”๏ธ manage.py์™€ ๊ฐ™์€ level์— pythonํŒŒ์ผ์„ ๋งŒ๋“ค์–ด ์—ฌ๊ธฐ์— ๋ฏผ๊ฐํ•œ ์ •๋ณด๋ฅผ ์ˆจ๊ธฐ๊ณ , ์ด ํŒŒ์ผ์„ .gitignore์— ์ถ”๊ฐ€์‹œํ‚ด์œผ๋กœ์จ ๋…ธ์ถœ์‹œํ‚ค์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค.
โœ”๏ธ ์ƒ์„ฑํ•œ ํŒŒ์ผ์— ์•„๋ž˜ ๋‚ด์šฉ์„ ๋ถ™์—ฌ๋„ฃ๊ณ , ์‹ค์ œ ์ •๋ณด์™€ ๋งคํ•‘ ์‹œํ‚จ๋‹ค.

# my_settings.py
DATABASES = {
    'default' : {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'DATABASE ๋ช…',  # ๐Ÿ‘ˆ mysql์—์„œ ์ƒ์„ฑํ•œ DB๋ช…
        'USER': '์‚ฌ์šฉ์ž๋ช…', # ๐Ÿ‘ˆ root๋ฅผ ์ ์–ด์ค€๋‹ค.
        'PASSWORD': 'DB์ ‘์†์šฉ ๋น„๋ฐ€๋ฒˆํ˜ธ', # ๐Ÿ‘ˆ mysql ๋น„๋ฐ€๋ฒˆํ˜ธ
        'HOST': '127.0.0.1',
        'PORT': '3306',
		'OPTIONS': {'charset': 'utf8mb4'}
    }
}
SECRET_KEY = 'settings.py์˜ secret_key' # ๐Ÿ‘ˆ SECRET_KEY

โœ”๏ธ settgins.py ์ตœ์ƒ๋‹จ์— ์œ„ ์ •๋ณด๊ฐ€ ์กด์žฌํ•˜๋Š” ํŒŒ์ผ์„ ๊ฐ€์ ธ์™€ ์—ฐ๋™์‹œํ‚จ๋‹ค.

from my_settings import DATABASES, SECRET_KEY
....
....
SECRET_KEY = SECRET_KEY  # ๐Ÿ‘ˆ ๊ธฐ์กด ์ •๋ณด ์‚ญ์ œ ํ›„, importํ•œ ๋ณ€์ˆ˜์™€ ์—ฐ๊ฒฐ์ง“๋Š”๋‹ค.
....
....
DATABASES = DATABASES  # ๐Ÿ‘ˆ sqllite3 ๊ด€๋ จ ์ •๋ณด ์‚ญ์ œ ํ›„, importํ•œ ๋ณ€์ˆ˜์™€ ์—ฐ๊ฒฐ์ง“๋Š”๋‹ค.
....
....

๐Ÿค” Application & MiddleWare

โœ”๏ธ Django ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ ๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ ์ค‘ ๋ถˆํ•„์š”ํ•œ ๊ธฐ๋Šฅ(admin, csrf, auth)์€ ์•„๋ž˜์ฒ˜๋Ÿผ ์ฃผ์„์ฒ˜๋ฆฌ ํ•ด์„œ ๊ฐ€๋ณ๊ฒŒ ๋งŒ๋“ค์–ด์ค€๋‹ค.
โœ”๏ธ Application์˜ ๋ณต์žก์„ฑ์„ ์ค„์ด๊ธฐ ์œ„ํ•ด 3๊ฐœ ํŒŒํŠธ๋กœ ๋ถ„๋ฅ˜ํ•˜์˜€๋‹ค.
โœ”๏ธ ๊ฐ€์ƒํ™˜๊ฒฝ ๋‚ด corsheaders ์„ค์น˜ ๋ฐ application & middleware ๋“ฑ๋ก

$ pip install django-cors-headers ๐Ÿ‘ˆ corsheaders

# Application definition
DJANGO_APPS = [
    # 'django.contrib.admin', # ๐Ÿ‘ˆ admin ๊ธฐ๋Šฅ ์ฃผ์„
    # 'django.contrib.auth', # ๐Ÿ‘ˆ auth ๊ธฐ๋Šฅ ์ฃผ์„
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
]
PROJECT_APPS = []
THIRD_PARTY_APPS = [
    "corsheaders",  # ๐Ÿ‘ˆ corsheaders ์•ฑ ๋“ฑ๋ก
]
INSTALLED_APPS = DJANGO_APPS + PROJECT_APPS + THIRD_PARTY_APPS
MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.common.CommonMiddleware",
    # 'django.middleware.csrf.CsrfViewMiddleware', # ๐Ÿ‘ˆ csrf ๊ธฐ๋Šฅ ์ฃผ์„
    # 'django.contrib.auth.middleware.AuthenticationMiddleware', # ๐Ÿ‘ˆ authentication ๊ธฐ๋Šฅ ์ฃผ์„
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
    "corsheaders.middleware.CorsMiddleware", # ๐Ÿ‘ˆ corsheaderMiddleWare ๋“ฑ๋ก
]

โœ”๏ธ settgins.py ์ตœํ•˜๋‹จ ๋ถ€์˜ CORS ๊ด€๋ จ ์„ค์ •์„ ์ถ”๊ฐ€์‹œํ‚จ๋‹ค.

##CORS
CORS_ORIGIN_ALLOW_ALL=True
CORS_ALLOW_CREDENTIALS = True
CORS_ALLOW_METHODS = (
    'DELETE',
    'GET',
    'OPTIONS',
    'PATCH',
    'POST',
    'PUT',
)
CORS_ALLOW_HEADERS = (
    'accept',
    'accept-encoding',
    'authorization',
    'content-type',
    'dnt',
    'origin',
    'user-agent',
    'x-csrftoken',
    'x-requested-with',
)

โœ”๏ธ ํ˜„์žฌ๊นŒ์ง€์˜ settgins์— ๋ฌธ์ œ๊ฐ€ ์—†๋Š”์ง€ ์ค‘๊ฐ„ ์ ๊ฒ€ ํ•œ๋‹ค. ์•„๋ž˜์™€ ๊ฐ™๋‹ค๋ฉด ์ •์ƒ์ด๋‹ค. ๋นจ๊ฐ„๊ธ€์”จ๋Š” ์•„์ง migrations๋ฅผ ์ง„ํ–‰ํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ˆ ๊ดœ์ฐฎ๋‹ค.

$ python manage.py runserver ๐Ÿ‘ˆ ์„œ๋ฒ„ ๊ตฌ๋™



4. git & .gitignore ์„ค์ •

๐Ÿค” .gitignore ํŒŒ์ผ ์ƒ์„ฑ

โœ”๏ธ manage.py๊ฐ€ ์žˆ๋Š” level์— .gitignore์„ ์ƒ์„ฑํ•œ๋‹ค.
โœ”๏ธ .gitignore์— ์ถ”๊ฐ€ํ•  ์ •๋ณด๋ฅผ "https://www.toptal.com/developers/gitignore" ์—์„œ ์ƒ์„ฑ ํ›„ ๋ถ™์—ฌ๋„ฃ๋Š”๋‹ค.
โœ”๏ธ python, pycharm, VisualStudioCode, vim, macOS, Linux, zsh ๊ด€๋ จ ์ •๋ณด๋ฅผ .gitignore์— ์ถ”๊ฐ€ํ•˜์˜€๋‹ค.
โœ”๏ธ ์ด์™€ ํ•จ๊ป˜ SECRET_KEY์™€ DATABASE ์ •๋ณด๋ฅผ ๋ถ„๋ฆฌ์‹œ์ผœ ๋‘” python ํŒŒ์ผ์„ .gitignore์— ์ถ”๊ฐ€ํ•œ๋‹ค.

# .gitignore
my_settings.py

๐Ÿค” git ๊ฐ„๋‹จ ๋ช…๋ น

$ git init ๐Ÿ‘ˆ git ์ดˆ๊ธฐํ™”

$ git remote add origin [github repository url] ๐Ÿ‘ˆ ์›๊ฒฉ repository ์—ฐ๊ฒฐ

$ git commit -am "๋‚ด์šฉ" ๐Ÿ‘ˆ add & commit

$ git branch -M main ๐Ÿ‘ˆ master ๋ช…์นญ main์œผ๋กœ ๋ณ€๊ฒฝ

$ git branch [branch๋ช…] ๐Ÿ‘ˆ branch ์ƒ์„ฑ

$ git checkout [branch๋ช…] ๐Ÿ‘ˆ ํ•ด๋‹น branch ์ „ํ™˜

$ git checkout -b [branch๋ช…] ๐Ÿ‘ˆ branch ์ƒ์„ฑ ํ›„ ์ด๋™

$ git push origin "๋ธŒ๋Ÿฐ์น˜๋ช…" ๐Ÿ‘ˆ github๋กœ push



๐Ÿ™ ๋Š๋‚€์ .

โœ”๏ธ ํ˜ผ์ž ๊ณต๋ถ€ํ•  ๋•Œ, django-admin startproejct [ํ”„๋กœ์ ํŠธ๋ช…] . ์„ ํ†ตํ•ด ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ–ˆ์—ˆ๋‹ค. ๋ณดํ†ต django-admin startproejct config . ๋กœ ๋งŒ๋“ค์—ˆ๋‹ค. project ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ app ๋””๋ ‰ํ† ๋ฆฌ์™€ ๊ฐ™์€ ๊ณ„์ธต์— ์œ„์น˜์‹œ์ผฐ์—ˆ๋Š”๋ฐ, ์ (.)์„ ๋ถ™์ด์ง€ ์•Š์œผ๋ฉด ๋ฐœ์ƒํ•˜๋Š” ๋””๋ ‰ํ† ๋ฆฌ์˜ ๊นŠ์ด ๋•Œ๋ฌธ์— ๋ถˆํŽธํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด๋ฒˆ์—๋„ ์ (.)์„ ์ด์šฉํ•ด ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ–ˆ๋‹ค๊ฐ€ ๋‹ค์‹œ ๋“€ํ† ๋ฆฌ์–ผ๋Œ€๋กœ ๋”ฐ๋ผ๊ฐ€๋ณด์•˜๋‹ค. ํŒ€ ํ”„๋กœ์ ํŠธ ์‹œ, ๋‹ค๋ฅธ ํŠธ๋ฆฌ๊ตฌ์กฐ๋กœ ์ธํ•œ ๋ฌธ์ œ๊ฐ€ ์—ผ๋ ค๋˜๊ธฐ๋„ํ•˜๊ณ , ํ•œ๋ฒˆ ๋” ์—ฐ์Šตํ•ด๋ณผ ์ƒ๊ฐ์œผ๋กœ ์ง„ํ–‰ํ–ˆ๋‹ค. ์ฒ˜์Œ์—๋Š” pipenv๊ฐ€ ๊ฐ€์žฅ ํŽธํ•˜๋‹ค๊ณ  ๋Š๊ผˆ๋Š”๋ฐ ์ด์ œ๋Š” miniconda๋กœ ๊ฐ€์ƒํ™˜๊ฒฝ์„ ํ™œ์„ฑํ™”ํ•˜์—ฌ ํ•˜๋Š” ์ž‘์—…์ด ์ ์ฐจ ์ต์ˆ™ํ•ด์ง€๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

โœ”๏ธ SECRET_KEY ๋“ฑ ์€ํ๊ฐ€ ํ•„์š”ํ•œ ์ •๋ณด๋ฅผ ๋ณ„๋„๋กœ ์ €์žฅํ•˜๊ณ  .gitignore์— ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•ด Django์—์„œ ์ œ๊ณตํ•˜๋Š” dotenv๋ฅผ ์‚ฌ์šฉํ–ˆ์—ˆ๋Š”๋ฐ, ๊ทธ๋ƒฅ ํŒŒ์ผ๋กœ ๋งŒ๋“ค์–ด import ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋” ๊ฐ„ํŽธํ•˜๊ฒŒ ๋Š๊ปด์กŒ๋‹ค. ๋ถ„๋ช… ์–ด๋–ค ์ฐจ์ด๊ฐ€ ์žˆ์„๊ฒƒ ๊ฐ™์€๋ฐ ์ฐพ์•„๋ด์•ผ๊ฒ ๋‹ค.

โœ”๏ธ ๋ช‡ ๋ฒˆ์˜ ๊ฐ„๋‹จํ•œ Django ํด๋ก ์ฝ”๋”ฉ์„ํ•˜๋ฉฐ, ํ”„๋ก ํŠธ์ชฝ๊ณผ ํ†ต์‹ ํ•˜๋Š” ์˜๋ฏธ์—๋Œ€ํ•ด์„œ '๋ฌด์—‡์„ ์–ด๋–ป๊ฒŒ ํ•˜๋Š”์ง€'์— ๋Œ€ํ•œ ์˜๋ฌธ์ด ์žˆ์—ˆ๋Š”๋ฐ, CORS์˜ ๋“ฑ์žฅ์œผ๋กœ ์–ด๋–ค ๊ฒƒ์„ ๊ณต๋ถ€ํ•ด์•ผํ• ์ง€ ์•Œ๊ฒŒ๋œ ๊ฒƒ๊ฐ™๋‹ค. SOP ๊ฐœ๋…๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ origin, fetch ๋“ฑ์— ๋Œ€ํ•ด ์•Œ์•„๋ด์•ผ๊ฒ ๋‹ค.

profile
Keep Going, Keep Coding!

1๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2021๋…„ 10์›” 15์ผ

์ด์ œ Django ์…‹ํŒ…์€ ์ด ํฌ์ŠคํŒ… ํ•˜๋‚˜๋กœ - ๋— - ๐Ÿ”ฅ๏ธ๐Ÿ”ฅ๏ธ๐Ÿ”ฅ๏ธ

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ