#2 INTRODUCTION TO DJANGO - WONILLISM/Clone_airbnb GitHub Wiki

created by wopark

์žฅ๊ณ  ํ”„๋กœ์ ํŠธ ๋งŒ๋“ค๊ธฐ

$ django-admin startproject config
  • rename config -> Aconfig

  • ์•ˆ์—์žˆ๋Š” configํด๋”๋ฅผ ๊บผ๋‚ธ ํ›„ Aconfig๋ฅผ ์‚ญ์ œ

  • ์ด ๋•Œ settings.py์— ์žˆ๋Š” secret_key๋ฅผ ๊ทธ๋ƒฅ ์˜ฌ๋ฆฌ๊ฒŒ ๋˜๋ฉด ๊นƒ์—์„œ ๋ณด์•ˆ ๊ฒฝ๊ณ  ๋ฉ”์ผ์ด ์˜จ๋‹ค.

  • ์ด๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•

    • secrets.json ํŒŒ์ผ ์ƒ์„ฑ
{
  "SECRET_KEY": "(1djli9wygoor+6^5g36bcn^ca+!+@8uc-=bw-3bk)z2pwrdq8"
}
  • gitignore์— secrets.json ์ถ”๊ฐ€
  • setting.py ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •
import os, json
from django.core.exceptions import ImproperlyConfigured

# 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_file = os.path.join(BASE_DIR, "secrets.json")

with open(secret_file) as f:
    secrets = json.loads(f.read())

def get_secret(setting, secrets=secrets):
    try:
        return secrets(setting)
    execept KeyError:
        error_msg = "Set the {} environment variable".format(setting)
        raise ImproperlyConfigured(error_msg)

SECRET_KEY = get_secret("SECRET_KEY")

...

python manage.py runserver -> ์„œ๋ฒ„ ๊ตฌ๋™
python mamage.py createsuperuser -> ๊ด€๋ฆฌ์ž ๊ณ„์ • ๋งŒ๋“ค๊ธฐ
python manage.py migrate -> ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—…๋ฐ์ดํŠธ

INSTALLED_APPS

  • django์™€ ํ•จ๊ป˜ ๋”ธ๋ ค์˜ค๋Š” ๊ธฐ๋ณธ ์•ฑ๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
    • django.contrib.admin
      • django์˜ ๊ฐ€์žฅ ๊ฐ•๋ ฅํ•œ ๋ถ€๋ถ„ ์ค‘ ํ•˜๋‚˜์ธ ์ž๋™ ๊ด€๋ฆฌ ์ธํ„ฐํŽ˜์ด์Šค์ด๋‹ค.
      • ๋ชจ๋ธ์—์„œ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด ์‹ ๋ขฐํ•  ์ˆ˜ ์žˆ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์‚ฌ์ดํŠธ์˜ ์ฝ˜ํ…์ธ ๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๋น ๋ฅธ ๋ชจ๋ธ ์ค‘์‹ฌ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•œ๋‹ค.
      • (์—ฐ๊ฒฐํ•œ url/admin/)์„ ํ†ตํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
    • django.contrib.auth
      • ์‚ฌ์šฉ์ž ์ธ์ฆ ์‹œ์Šคํ…œ์„ ์ง€์›ํ•œ๋‹ค.
      • ์‚ฌ์šฉ์ž ๊ณ„์ •, ๊ทธ๋ฃน, ๊ถŒํ•œ๊ณผ ์ฟ ํ‚ค ๊ธฐ๋ฐ˜์˜ ์‚ฌ์šฉ์ž, ์„ธ์…˜์„ ๋‹ค๋ฃฌ๋‹ค.
    • django.contrib.contenttypes
      • django ๊ธฐ๋ฐ˜ ํ”„๋กœ์ ํŠธ์— ์„ค์น˜๋œ ๋ชจ๋“  ๋ชจ๋ธ์„ ์ถ”์ ํ•  ์ˆ˜ ์žˆ๋Š” contenttypes ์•ฑ์ด ํฌํ•จ๋˜์–ด ์žˆ์–ด ๋ชจ๋ธ ์ž‘์—…์„ ์œ„ํ•œ ๊ณ ๊ธ‰, ์ผ๋ฐ˜ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•œ๋‹ค.
    • django.contrib.sessions
      • ์„ธ์…˜ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‚ฌ์ดํŠธ ๋ฐฉ๋ฌธ์ž๋ณ„๋กœ ์ž„์˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ์„œ๋ฒ„์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ์ฟ ํ‚ค์˜ ์†ก์ˆ˜์‹ ์„ ์ถ”์ƒํ™”ํ•œ๋‹ค.
    • django.contrib.messages
      • ์ต๋ช… ๋ฐ ์ธ์ฆ๋œ ์‚ฌ์šฉ์ž ๋ชจ๋‘์—๊ฒŒ ์ฟ ํ‚ค ๋ฐ ์„ธ์…˜ ๊ธฐ๋ฐ˜ ๋ฉ”์‹œ์ง•์„ ์™„๋ฒฝํ•˜๊ฒŒ ์ง€์›ํ•œ๋‹ค.
    • django.contrib.staticfiles
      • ์ •์ ํŒŒ์ผ์„ ๊ด€๋ฆฌํ•˜๋Š” ํ”„๋ ˆ์ž„์›Œํฌ

django-admin startapp ์•ฑ์ด๋ฆ„

  • makemigrations

    • ์ƒ์„ฑ / ์ˆ˜์ •๋œ ๋ชจ๋ธ๋“ค์˜ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ migrations๋กœ ์ €์žฅํ•˜๊ณ ์ž Django์—๊ฒŒ ์•Œ๋ ค์ค€๋‹ค.
  • migrate

    • makemigrations ๋˜๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ƒ์„ฑ๋œ migrations๋“ค์„ ์‹คํ–‰์‹œ์ผœ์ฃผ๊ณ  ์ž๋™์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์Šคํ‚ค๋งˆ๋ฅผ ๊ด€๋ฆฌํ•ด์ค€๋‹ค.
  • createsuperuser

    • ๋ชจ๋“  ๊ถŒํ•œ์„ ๊ฐ€์ง€๋Š” superuser๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
  • django-admin startapp app ์ด๋ฆ„

    • conversations
    • lists
    • reservations
    • reviews
    • rooms
    • users
      • django์—๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ user๊ฐ€ ์กด์žฌํ•˜์ง€๋งŒ, ์ด user๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ‘๊ทผ ํ•  ์ˆ˜ ์žˆ๋Š” user๋ฅผ ๋œปํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์šฐ๋ฆฌ๊ฐ€ ํ•„์š”ํ•œ user๋Š” ํ™ˆํŽ˜์ด์ง€๋ฅผ ์‚ฌ์šฉํ•  ๊ณ ๊ฐ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ‘๊ทผํ•˜์ง€ ๋ชปํ•˜๋Š” ์ƒˆ๋กœ์šด users๋ฅผ ์ƒ์„ฑํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.
      • app์„ ์ƒ์„ฑํ–ˆ์„๋•Œ ๋งŒ๋“ค์–ด์ง€๋Š” ๊ธฐ๋ณธ์ ์ธ ํŒŒ์ผ ๋ฐ ํด๋”๋“ค์€ django์—๊ฒŒ ํ•„์š”ํ•œ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์‚ญ์ œํ•˜๊ฑฐ๋‚˜ ์ด๋ฆ„์„ ๋ฐ”๊พธ์–ด์„œ๋Š” ์•ˆ๋œ๋‹ค.

์ƒ์„ฑํ•œ app์•ˆ์˜ ํŒŒ์ผ๋“ค

  • __init__.py
  • admin.py
    • app์˜ ๋‚ด์šฉ๋“ค์„ admin ํŒจ๋„์— ๋ณด์—ฌ์ฃผ๊ธฐ์œ„ํ•œ ํŒŒ์ผ
  • veiw.py
    • ํŽ˜์ด์ง€์—์„œ ๋ณด์—ฌ์งˆ ๋ถ€๋ถ„๋“ค์„ ์ž‘์„ฑํ•˜๋Š” ํŒŒ์ผ -> html๋ Œ๋”
  • urls.py
    • url๋“ค์„ ๊ด€๋ฆฌํ•˜๋Š” ํŒŒ์ผ
    • config/urls.py ์— ๋ชจ๋“  ์•ฑ๋“ค์˜ url์„ ๊ด€๋ฆฌํ•˜์ง€์•Š๊ณ  ๊ฐ ์•ฑ์— ์žˆ๋Š” urls.py๋งŒ๋“ค์–ด์„œ ์—ฐ๋™์‹œ์ผœ์ค€๋‹ค.
  • apps.py
    • ์•ฑ์˜ ๊ตฌ์„ฑ์„ ๊ด€๋ฆฌํ•˜๋Š” ํŒŒ์ผ
  • models.py
    • ์•ฑ์˜ ๋ชจ๋ธ์„ ๊ด€๋ฆฌํ•˜๋Š” ํŒŒ์ผ(๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค) -> ์•ฑ์˜ ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ
  • tests.py

created by mki

#2.0 Creating a Django Project (05:46)

์ฒซ ๋ฒˆ์งธ django ํ”„๋กœ์ ํŠธ ์ž‘์„ฑํ•˜๊ธฐ

์ด์ „์— ์•„๋ž˜์˜ ๋ช…๋ น์œผ๋กœ pipenvํ™˜๊ฒฝ์„ ๋งŒ๋“ ๋‹ค๋ฉด PipfileํŒŒ์ผ์ด ์ƒ์„ฑ๋œ๋‹ค. ๊ทธ ๋‹ค์Œ README.md์™€ .gitignore์„ ๋งŒ๋“ค์ž.

mkdir airbnb-clone
mv airbnb-clone
pipenv --python 3.7
pipenv install django==2.2.5

์ด์ œ ์‹ค์ œ๋กœ djangoํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•ด๋ณด์ž. ์žฅ๊ณ  ํŠœํ† ๋ฆฌ์–ผ์—์„œ๋Š” ์•„๋ž˜์™€ ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค๋ฉด ๋œ๋‹ค๊ณ  ํ•œ๋‹ค.

https://docs.djangoproject.com/ko/3.2/intro/tutorial01/

django-admin startproject mysite

๊ทธ๋Ÿฌ๋‚˜ ์œ„ ๋ช…๋ น์–ด๋Š” ์ดˆ์‹ฌ์ž๋ฅผ ์œ„ํ•œ ๊ฒƒ์ด๊ณ , ๊ทœ๋ชจ๋ฅผ ํ™•์žฅํ•˜๊ธฐ์—๋Š” ๋ณ„๋กœ์ด๋‹ค. ๋”ฐ๋ผ์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์‹œ์ž‘ํ•˜์ž.

django-admin startporject config

๊ทธ ๋‹ค์Œ ์•„๋ž˜ ๋ช…๋ น์„ ์‹คํ–‰ํ•ด configํด๋” ์•ˆ์— ์žˆ๋Š” configํด๋”์™€ manage.py๋ฅผ ๋ฐ–์œผ๋กœ ์˜ฎ๊ธฐ์ž.

mv config Aconfig
mv Aconfig/config Aconfig/manage.py .
rm -rf Aconfig

๋ฒˆ๊ฑฐ๋กœ์šด ๊ณผ์ •์„ ํ”ผํ•˜๋ ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•˜์ž.

django-admin startproject config .

์•„๋ž˜ ์ฑ…์—์„œ๋„ ์ด๋Ÿฌํ•œ ๋ฐฉ๋ฒ•์„ ์ถ”์ฒœํ•˜๊ณ  ์žˆ๋‹ค.

https://www.amazon.com/Two-Scoops-Django-1-11-Practices-ebook/dp/B076D5FKFX

Interpreter ์„ ํƒ

VSC๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ ๋„์›€์ด ๋˜๋Š”Extension๋“ค์„ ๋งŽ์ด ์ œ๊ณตํ•˜๊ณ  ์žˆ๋‹ค. Python์—ญ์‹œ Microsoft์—์„œ ์ง์ ‘ ์ œ๊ณต์„ ํ•˜๊ณ  ์žˆ๋‹ค. ์„ค์น˜๋ฅผ ํ•œ ๋’ค์—, VCS์˜ ๊ฐ€์žฅ ์•„๋ž˜ ์ƒํƒœ๋ฐ”๋ฅผ ๋ณด๋ฉด, Python 2.7.10 64-bit๋กœ ์„ ํƒ๋˜์–ด ์žˆ๋‹ค. ์ด๊ฒƒ์„ 'airbnb-clone': pipenv๊ฐ€ ์ ํ˜€์žˆ๋Š” python์„ ์„ ํƒํ•˜๋ฉด ์—๋Ÿฌ๋ฉ”์‹œ์ง€๊ฐ€ ์‚ฌ๋ผ์ง„๋‹ค.

COMMAND+SHIFT+P๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‰ฝ๊ฒŒ ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

ํ‚ค์›Œ๋“œ

  • django-admin
  • startproject

#2.1 Setting up VSCode and Python Like a PRO (10:08)

Linter

ํŒŒ์ด์ฌ์€ ์ปดํŒŒ์ผ ์–ธ์–ด๊ฐ€ ์•„๋‹ˆ๊ณ , ๋Ÿฐํƒ€์ž„ ์–ธ์–ด์ด๋‹ค. ๋”ฐ๋Ÿฌ์„œ ์ปดํŒŒ์ผ ์–ธ์–ด์ฒ˜๋Ÿผ ์—๋Ÿฌ๋ฅผ ์‚ฌ์ „์— ์žก์•„์ฃผ์ง€ ์•Š๋Š”๋‹ค.

Linter๋Š” ์—ฌ๋Ÿฌ๋ถ„๋“ค์ด ์“ด ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด์„œ ์—๋Ÿฌ๊ฐ€ ์ƒ๊ธธ ๋ถ€๋ถ„์„ ๋ฏธ๋ฆฌ ๊ฐ์ง€ํ•˜๋Š” ํ™•์žฅํ”„๋กœ๊ทธ๋žจ์ด๋‹ค. ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ  ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ฑฐ๋‚˜, ์—†๋Š” ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์•ˆ๋˜๋Š”๋ฐ, ์ด๋Ÿฌํ•œ ๋ถ€๋ถ„์„ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์ „์— ๋ฏธ๋ฆฌ ๊ฐ์ง€ํ•ด์„œ ์•Œ๋ ค์ค€๋‹ค.

https://ko.wikipedia.org/wiki/๋ฆฐํŠธ_(์†Œํ”„ํŠธ์›จ์–ด)

๋˜ํ•œ Linter๋Š” ํŒŒ์ด์ฌ์—์„œ ๋„๋ฆฌ ์“ฐ์ด๋Š” ๊ด€์Šต๋„ ์ค€์ˆ˜ํ•œ๋‹ค. ์ด๋Ÿฌํ•œ ๊ด€์Šต์„ ๋‚˜์—ดํ•œ ๊ฒƒ์„ PEP๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

PEP

https://www.python.org/dev/peps/

์ด์ค‘์—์„œ PEP8์€ ์ฝ”๋“œ๋ฅผ ์–ด๋–ป๊ฒŒ ์ž‘์„ฑํ•ด์•ผ ํ•˜๋Š”์ง€ ๋ฌธ์„œํ™”ํ•œ ๊ฒƒ์ด๋‹ค.

https://www.python.org/dev/peps/pep-0008/

์ด๋Ÿฌํ•œ ๊ด€์Šต์„ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š๋„๋ก ๊ด€๋ฆฌํ•ด ์ฃผ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ Linter๋ผ๊ณ  ํ•œ๋‹ค. ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค๋ฉด, ๋ฌธ์ œ๊ฐ€ ๋˜๋Š” ๋ถ€๋ถ„์„ ์•Œ๋ ค์ค€๋‹ค. ์ •๋ง ๋งŽ์€ Linter๊ฐ€ ์žˆ๋Š”๋ฐ, ๊ฐ€์žฅ ์„ ํ˜ธ๋˜๋Š” Linter๋Š” flake8์ด๋‹ค. pylint๋ผ๋Š” ๊ฒƒ๋„ ์žˆ๋‹ค.

flake8

https://pypi.org/project/flake8/

VSCํŒ์—…์—์„œ ์–ด๋–ค linter๋ฅผ ์„ ํƒํ• ์ง€ ๋‚˜์˜ค๋ฉด flake8์„ ์„ ํƒํ•˜์ž.

๊ทธ๋Ÿฌ๋ฉด .vscodeํด๋”์™€ ํ•จ๊ป˜ ๊ทธ ์•ˆ์— settings.jsonํŒŒ์ผ์ด ๋งŒ๋“ค์–ด์ง„๋‹ค.

# .vscode/setting.json

{
	"python.pythonPath": "~/.local/share/virtualenvs/mki-934kEbld/"
	"python.linting.flake8Enabled": true,
	"python.linting.pylintEnabled": false,
	"python.linting.enabled": true
}

์œ„์™€ ๊ฐ™์ด ๋งŒ๋“ค์–ด์ง€์ง€ ์•Š์•˜๋‹ค๋ฉด, ์ƒˆ๋กœ ์ถ”๊ฐ€ํ•˜์ž. ๊ทธ ๋‹ค์Œ flake8์„ ์„ค์น˜ํ•˜๋ผ๋Š” ํŒ์—…์ด ๋œฌ๋‹ค. ์„ค์น˜๋ฅผ ์ง„ํ–‰ํ•˜์ž.

#COMMAND + SHIFT + P
#Python: Select liner
pipenv install flake8 --dev

Pipfile์„ ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ ํ˜€์žˆ๋Š”๋ฐ, packages๋Š” ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ๋™์ž‘ํ•  ๋•Œ ํ•„์š”ํ•œ ํŒจํ‚ค์ง€์ด๊ณ , dev-packages๋Š” ๊ฐœ๋ฐœํ•  ๋•Œ ํ•„์š”ํ•œ ํŒจํ‚ค์ง€์ด๋‹ค.

# Pipfile

[source](/WONILLISM/Clone_airbnb/wiki/source)
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
django = "==2.2.5"
pillow = "*"
django-countries = "*"

[dev-packages]
flake8 = "*"
black = "*"

[requires]
python_version = "3.7"

[pipenv]
allow_prereleases = true

Code Formatting

http://c2.com/ppr/formatting.html

Formatter์€ ์ฝ”๋“œ๋ฅผ ์ž๋™์œผ๋กœ ํฌ๋ฉงํŒ…ํ•ด์ฃผ๋Š” ํ”„๋กœ๊ทธ๋žจ์ด๋‹ค. Linter๊ฐ€ ์˜ค๋ฅ˜๊ฐ€ ๋‚  ๋ถ€๋ถ„์„ ์•Œ๋ ค์ค€๋‹ค๋ฉด, Formatter๋Š” ์‹ค์ œ๋กœ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด์ค€๋‹ค.

black

black์ด๋ž€ formatter๋ฅผ ์ถ”์ฒœํ•œ๋‹ค.

https://pypi.org/project/black/

https://www.daleseo.com/python-black/

์„ค์น˜ํ•˜๋Š” ๋ฒ•์€ ํŒ์—…์„ ๋ˆŒ๋Ÿฌ๋„ ๋˜๊ณ , ์•„๋ž˜์™€ ๊ฐ™์ด ์ž…๋ ฅํ•ด๋„ ๋œ๋‹ค.

pipenv install black --dev --pre

flake8 + black์€ ์ตœ๊ณ ์˜ ์กฐํ•ฉ์ด๋‹ค. ์„ค์น˜๋ฅผ ํ•˜๋ฉด VSC์•„๋ž˜์— ์ƒˆ๋กœ์šด ์ƒํƒœ์ฐฝ์ด ์ƒ์„ฑ๋˜๋Š”๋ฐ, ์ €์žฅํ•  ๋•Œ๋งˆ๋‹ค ๋ฌด์–ธ๊ฐ€ ๋ฐ”๋€Œ๋Š” ๊ฒƒ์ด ๋ณด์ผ ๊ฒƒ์ด๋‹ค. ๋งŒ์•ฝ ๋ณด์ด์ง€ ์•Š์œผ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์‹คํ–‰ํ•˜์ž.

  • settingโ†’"format on save"โ†’check

๋งŒ์•ฝ ์ด๋Ÿฌํ•œ ์ฒดํฌ ๋ฐ•์Šค๊ฐ€ ์—†๋‹ค๋ฉด setting.json์„ ์ˆ˜์ •ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

{
	"editor.formatOnSave": true,
}

Line ๊ธธ์ด ์ œํ•œ ์ˆ˜์ •

ํŒŒ์ด์ฌ์€ ๋ผ์ธ์˜ ๊ธธ์ด๊ฐ€ 79์ž๋ฅผ ๋„˜์–ด์„œ๋ฉด ์•ˆ๋œ๋‹ค. E501์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š”๋ฐ, ์˜ˆ์ „์—๋Š” ๋ชจ๋‹ˆํ„ฐ๊ฐ€ ์ž‘์•„์„œ ์–ด์ฉ” ์ˆ˜ ์—†์—ˆ์ง€๋งŒ, ์ง€๊ธˆ์€ ๋ชจ๋‹ˆํ„ฐ๊ฐ€ ํฌ๋‹ค. ๊ทธ๋ž˜์„œ ์ด๋Ÿฐ ์ œํ•œ์ด ์—†์–ด๋„ ๋œ๋‹ค. ๊ทธ๋ž˜์„œ ์•„๋ž˜์™€ ๊ฐ™์ด setting.jsonํŒŒ์ผ์„ ์ˆ˜์ •ํ•ด์ฃผ์ž.

{
	"python.linting.flake8Args": ["--max-line-length=88"]
}

setting.json

์ง€๊ธˆ๊นŒ์ง€ ์ž‘์—…ํ•œ setting.json์„ ํ•œ ๋ˆˆ์— ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

{
    "python.pythonPath": "~/.local/share/virtualenvs/mki-934kEbld/",
    "python.linting.flake8Enabled": true,
    "python.linting.pylintEnabled": false,
    "python.linting.enabled": true,
    "python.formatting.provider": "black",
    "python.linting.flake8Args": [
        "--max-line-length=88"
    ],
}

#2.2 Falling in love with Django part One (06:47)

ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“  ๋’ค์— ์ƒ์„ฑ๋œ ํŒŒ์ผ๋“ค์„ ์‚ดํŽด๋ณด์ž. ์šฐ์„  manage.py๊ฐ€ ์žˆ๋‹ค.

manage.py

https://docs.djangoproject.com/ko/3.2/ref/django-admin/

https://devlog.jwgo.kr/2018/02/07/what-is-the-diff-btw-manage-django-admin/

config/init.py

init.py๋Š” ์žฅ๊ณ ๊ฐ€ ์•„๋‹ˆ๋ผ ํŒŒ์ด์ฌํ•œํ…Œ ํ•„์š”ํ•œ ๊ฒƒ์ด๋‹ค. ์ƒˆ๋กœ์šด ํด๋”๋ฅผ ๋งŒ๋“  ๋’ค,๊ทธ ํด๋”์˜ ํŒŒ์ผ๋“ค์„ ์‚ฌ์šฉํ•  ๋•Œ์—, ํŒŒ์ด์ฌ์€ ํ•ญ์ƒ init.py๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ๊ทธ๋ž˜์•ผ ํŒŒ์ด์„  ํŒจํ‚ค์ง€๋กœ ์ธ์‹ํ•ด์„œ ํŒŒ์ผ์„ importํ•  ์ˆ˜ ์žˆ๋‹ค.

https://wikidocs.net/1418

https://velog.io/@limes/Python-init.py-ํŒŒ์ผ์˜-์—ญํ• 

config/setting.py

์žฅ๊ณ ๊ฐ€ ๋งŒ๋“ค์–ด ๋†“์€ ๊ฒƒ.

INSTALLED_APPS

MIDDLEWARE

TEMPLATES

WSGI_APPLICATION

DATABASES

AUTO_PASSWORD_VALIDATORS

๋ชจ๋“  ๊ฒƒ์ด ์ค€๋น„๊ฐ€ ๋˜์–ด์žˆ๋‹ค.

TIME_ZONE = "Asia/Seoul"

๋งจ ์œ„์˜ ์ฃผ์„์€ ๋งค์šฐ ์ค‘์š”ํ•˜๋‹ˆ ๊ผญ ์ฝ์–ด๋ผ. ๋งค์šฐ ๋„์›€์ด ๋˜๋ฉฐ, ๋งํฌ๋„ ๋งค์šฐ ๋„์›€์ด ๋œ๋‹ค.

https://docs.djangoproject.com/ko/3.2/topics/settings/

config/urls.py

config/wsgi.py

Django Documentation

์—ฌ๋Ÿฌ๋ถ„์˜ ์ƒˆ๋กœ์šด ๋ฒ ํ”„

์žฅ๊ณ ์˜ ์ฒซ๋ฒˆ์งธ ์ข‹์€ ์ 

Django์— ํ•„์š”ํ•œ ๋ชจ๋“  ๊ฒƒ์ด ์—ฌ๊ธฐ์— ์žˆ๋‹ค.

์„ธํŒ… ํŒŒ์ผ์„ ๋ณด๊ณ  ๋ชจ๋ฅด๋ฉด ๋งํฌ๋ฅผ ์—ด์–ด์„œ ๋ณด์ž.

์žฅ๊ณ ๋Š” ๋ชจ๋“  ๊ฒƒ์„ ์ค€๋น„ํ•ด ๋†“์•˜๊ณ , ์†Œ์Šค์ฝ”๋“œ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๊ณ , ์ฝ”๋ฉ˜ํŠธ๋„ ์นœ์ ˆํ•˜๊ณ  ๋„ํ๋จผํŠธ๋„ ์นœ์ ˆํ•˜๋‹ค.

#2.3 Falling in love with Django part Two (09:39)

manage.py

Commandํ‚ค๋ฅผ ๊พน ๋ˆ„๋ฅด๋ฉด function์ •์˜ ๋ถ€๋ถ„์œผ๋กœ ๊ฐ„๋‹ค.

intellisence

https://code.visualstudio.com/docs/editor/intellisense

python manage.py runserver

https://docs.djangoproject.com/ko/3.2/ref/django-admin/#django-admin-runserver

logger

ํŒŒ์ผ์„ ์ €์žฅํ•˜๋Š” ์ˆœ๊ฐ„ ์ฆ‰์‹œ ๋‹ค์‹œ ์‹คํ–‰์ด ๋œ๋‹ค.

db.sqlite3 ๊ฐœ๋ฐœ์šฉ ๋””๋น„ ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋จ.

127.0.0.1:8000/admin

์‹คํ–‰์ด ์•ˆ๋œ๋‹ค.

ctrl + z

python manage.py migrate

https://docs.djangoproject.com/ko/3.2/ref/django-admin/#django-admin-migrate

python manage.py runserver

127.0.0.1:8000/admin

์‚ฌ์šฉ์ž๊ฐ€ ์—†๋‹ค.

python manage.py createsuperuser

๋‹ค์‹œ ๋“ค์–ด๊ฐ€์„œ ์‚ฌ์šฉ์ž mki ๋น„๋ฐ€๋ฒˆํ˜ธ์ž…๋ ฅ

#2.4 Django Migrations (09:12)

python manage.py makemigrations

https://docs.djangoproject.com/ko/3.2/ref/django-admin/#makemigrations

python manage.py migrate

https://docs.djangoproject.com/ko/3.2/ref/django-admin/#django-admin-migrate

db.sqlite3์‚ญ์ œ

unapplied migration ์—๋Ÿฌ๋ฉ”์‹œ์ง€

์žฅ๊ณ ์™€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ์—ฐ๋™๋˜์–ด์žˆ์ง€ ์•Š๋‹ค.

SQLite3์€ ์†Œ๊ทœ๋ชจ ํ”„๋กœ์ ํŠธ์—์„œ ์‚ฌ์šฉ

PostgreSQL

์šฐ๋ฆฌ์˜ ์˜ต์…˜ 2๊ฐœ.

SQL์„ ๊ณต๋ถ€ํ•ด์„œ ์‚ฌ์šฉํ•˜์ž.

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋ฌด์—‡์„ ๋„ฃ์„์ง€ ์ •ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

์žฅ๊ณ ์—๋Š” migration system์ด ์žˆ๋‹ค.

๋ฌด์–ธ๊ฐ€๋ฅผ ํŠน์ • ์žฅ์†Œ์—์„œ ๋‹ค๋ฅธ ๊ณณ์œผ๋กœ ์ด๋™์‹œํ‚ค๋Š” ๊ฒƒ์€.

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๊ฒฝ์šฐ ํ•˜๋‚˜์˜ ์ƒํƒœ์—์„œ ๋‹ค๋ฅธ ์ƒํƒœ๋กœ ์˜ฎ๊ธฐ๋Š” ๊ฒƒ.

๋ฐ์ดํ„ฐ ์œ ํ˜•์ด ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค migration์ด ํ•„์š”ํ•˜๋‹ค.

python manage.py makemigrations

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์žฅ๊ณ ๋ฅผ ๋™๊ธฐํ™”...

python manage.py migrate

#2.5 Django Applications (09:09)

์–ด๋””์— ๋ฌด์—‡์„ ๋„ฃ์„ ๊ฒƒ์ธ๊ฐ€?

project๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ชจ์Œ.

์–ดํ”Œ์€ function์˜ ๋ชจ์Œ

room์ด๋ž€ ๊ณตํ†ต. ์ƒ์„ฑ ์ถ”๊ฐ€ ์‚ญ์ œ

review๊ธฐ๋Šฅ.

์—„์ฒญ ๋งŽ์€ ๊ธฐ๋Šฅ๋“ค์„ ์–ด๋Š ํด๋”์— ๋„ฃ์„๊นŒ?

๊ฑฐ๋Œ€ํ•˜๊ณ  ์–ด๋งˆ๋ฌด์‹œํ•œ ํด๋”๋ฅผ ๋งŒ๋“ค๋ฉด ์•ˆ๋œ๋‹ค.

Divide and conquer

๊ณ„ํš์„ ํ•˜๊ณ  ์‹œ์ž‘ํ•ด์•ผ ํ•œ๋‹ค.

์ž‘์€ ํด๋”๋“ค์„ ์—ฌ๋Ÿฌ๊ฐœ ๋งŒ๋“ค์–ด์„œ config์— importํ•  ๊ฒƒ์ด๋‹ค. ์—ฌ๋Ÿฌ ๋งค์šฐ ์ž‘์€ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜๋“ค์˜ ๋ชจ์Œ.

์–ธ์ œ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งŒ๋“ค๊ณ  ์•ˆ๋งŒ๋“ค์ง€ ์ •ํ•ด์•ผ ํ•œ๋‹ค.

ํ•œ ๋ฌธ์žฅ์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜! and, or๋กœ ์„ค๋ช…ํ•˜๋ฉด ์•ˆ๋œ๋‹ค.

ํ•ญ์ƒ ์ž‘๊ฒŒ ๋‚˜๋ˆ„๊ธฐ!

#2.6 Creating the Apps! (06:16)

startapp

https://docs.djangoproject.com/ko/3.2/ref/django-admin/#startapp

airbnb-clone์•ˆ, configํด๋” ๋ฐ–์—์„œ ์•„๋ž˜ ๋ช…๋ น์„ ์‹คํ–‰ํ•˜์ž.

django-admin startapp rooms

ํ•˜๋‚˜ ๋” ํ•ด๋ณด์ž

django-admin startapp users
django-admin startapp reviews
django-admin startapp conversations
django-admin startapp lists
django-admin startapp reservations

application์„ ์ถ”๊ฐ€ํ•ด์•ผ ๊ด€๋ฆฌ์ž์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

django user

#2.7 Explaining the Apps (07:38)

๋งŒ๋“  application์•ˆ์— ์žˆ๋Š” ํด๋”์™€ ํŒŒ์ผ๋“ค์˜ ์ด๋ฆ„์„ ์ ˆ๋Œ€ ๋ณ€๊ฒฝํ•ด์„œ๋Š” ์•ˆ๋œ๋‹ค.

framework์™€ library์˜ ์ฐจ์ด์ ์€, framework๋Š” ๋ฃฐ์„ ๋”ฐ๋ผ์•ผ ํ•œ๋‹ค. react๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ! ์ด๋ฆ„๋“ค์€ optional์ด ์•„๋‹ˆ๋‹ค!

์žฅ๊ณ ๋Š” urls.py๋ฅผ ์ง€์ผœ๋ณด๊ณ  ์žˆ๋‹ค.

framework๋ฃฐ์„ ๋”ฐ๋ผ์•ผ ํ•œ๋‹ค!

migrations

init.py

admin.py

adminํŒจ๋„์— ์ ์šฉ๋˜๋Š” ๊ฒƒ๋“ค

apps.py

configurations...?

models.py

data

database

tests.py

views.py

์‚ฌ์šฉ์ž๋“ค์ด ๋ณด๋Š” ๊ฒƒ

html render, form upload

urls.py

applicationํด๋” ์•ˆ์— urls๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค์–ด์„œ url๋“ค์„ ๋ถ„ํ• ์ •๋ณตํ•˜์ž!