TIL-031 | Django_Project Setting

Lee, Chankyuยท2021๋…„ 10์›” 24์ผ
0
post-thumbnail
post-custom-banner

๐ŸŒˆ Django

๋””์ž์ธ ํŒจํ„ด ๊ด€๋ จ ์ง€๋‚œ ๊ธ€ ๋งํฌ

  • ๊ฐ€์ƒํ™˜๊ฒฝ ์ƒ์„ฑ(๋ฏธ๋‹ˆ์ฝ˜๋‹ค)

    • ์‹ ๊ทœ ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•  ๋•Œ ๋Œ€๋ถ€๋ถ„ ๊ฐ€์ƒํ™˜๊ฒฝ ์ƒ์„ฑ์„ ์ง„ํ–‰ ํ•œ๋‹ค. ์ง„ํ–‰ํ•˜๋Š” ํ”„๋กœ์ ํŠธ๊ฐ€ 1๊ฐœ ๋ฟ์ด๋ผ๋ฉด ๋ฌด๊ด€ํ•˜๊ฒ ์ง€๋งŒ, ๋‹ค์ˆ˜์˜ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•  ์‹œ ํ”„๋กœ์ ํŠธ์˜ ์‹œ์ž‘์‹œ์ , ๋ชฉ์  ๋“ฑ์— ๋”ฐ๋ผ ์–ธ์–ด, ํ”„๋ ˆ์ž„์›Œํฌ์˜ ๋ฒ„์ „์ด ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฒ„์ „ ๊ฐ„ ์ถฉ๋Œ์„ ์˜ˆ๋ฐฉํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค.

      conda create -n "๊ฐ€์ƒํ™˜๊ฒฝ ์ด๋ฆ„" python=3.8
      conda activate "๊ฐ€์ƒํ™˜๊ฒฝ ์ด๋ฆ„"

  • Database(mySQL) ์ƒ์„ฑ

    • mySQL ์‹คํ–‰ํ•˜์—ฌ database๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
    • character set์„ utf8mb4๋กœ ์ง€์ •ํ•  ๊ฒฝ์šฐ ์ด๋ชจ์ง€์˜ ์ž…๋ ฅ์ด ๊ฐ€๋Šฅํ•ด์ง„๋‹ค. ์ผ๋ฐ˜์ ์ด ๋งŽ์ด ์“ฐ์ด๋Š” utf8์€ ์ด๋ชจ์ง€ ์‚ฌ์šฉ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.
    • collation์€ database์—์„œ ๋ฌธ์ž์—ด์„ ์ •๋ ฌํ• ๋•Œ ์–ด๋–ค ๋ฌธ์ž๋“ค์ด ๋จผ์ €์˜ฌ์ง€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๊ธฐ์ค€์„ ์˜๋ฏธํ•œ๋‹ค.(ex. ABCabc,AaBb...)
      collation์€ utf8mb4_general_ci ์ด ๊ธฐ๋ณธ๊ฐ’์ด๋‹ค.

      $ mysql -u root -p

      mysql> create database NAME character set utf8mb4 collate utf8mb4_general_ci;

  • Django ์„ค์น˜

    • ์œ„์—์„œ ์„ค์ •ํ•œ ๊ฐ€์ƒํ™˜๊ฒฝ์— Django์™€ mysqlclient๋ฅผ ์„ค์น˜ํ•œ๋‹ค.

      $ pip install django

      $ pip install mysqlclient
      --> ์ดํ›„ MySQL server์— ์ ‘์†ํ•˜๊ธฐ ์œ„ํ•œ package

  • Django ํ”„๋กœ์ ํŠธ ์„ค์ •

    • ์‹œ์ž‘ํ•˜๊ณ ์ž ํ•˜๋Š” ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•œ ํ›„ ํ•ด๋‹น ๋””๋ ‰ํ† ๋ฆฌ์— ๋“ค์–ด๊ฐ„๋‹ค.

      $ django-admin startproject ํ”„๋กœ์ ํŠธ๋ช…
      $ cd ํ”„๋กœ์ ํŠธ๋ช…

  • settings.py ์„ค์ •

    • IP ํ—ˆ์šฉ
      : ๋””๋ฒ„๊น… ๋ชจ๋“œ์—์„œ ๋น„์–ด์žˆ๋Š” ALLOWED_HOSTS ๋ฆฌ์ŠคํŠธ๋Š” ['localhost', '127.0.0.1', '[::1]'] ์˜๋ฏธ๊ฐ€ ๋œ๋‹ค(๋กœ์ปฌ ํ˜ธ์ŠคํŠธ์—์„œ๋งŒ ์ ‘์†์ด ๊ฐ€๋Šฅ). ๋””๋ฒ„๊น… ๋ชจ๋“œ๋ฅผ ๋„๋ฉด ์ง€์ •ํ•œ ํ˜ธ์ŠคํŠธ์—๋งŒ ์ ‘์†ํ•  ์ˆ˜ ์žˆ๋‹ค.

      ALLOWED_HOSTS = ['*']

    • ์ฃผ์„์ฒ˜๋ฆฌ
      : ์ž˜ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” admin page ๊ธฐ๋Šฅ์— ๊ด€๋ จ๋œ ์ฝ”๋“œ๋ฅผ ์ฃผ์„์ฒ˜๋ฆฌ ํ•œ๋‹ค.

      urls.py ์˜ ๋‚ด์šฉ๋„ ์ˆ˜์ •!

      from django.urls import path
      
      urlpatterns = [
      ]
  • my_settings.py ํŒŒ์ผ ์ƒ์„ฑ

    • manage.py ํŒŒ์ผ๊ณผ ๋™์ผํ•œ ์œ„์น˜์— my_settings.py ๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค.

    • settings.py ์—์„œ ๋…ธ์ถœ๋˜๋ฉด ์•ˆ๋˜๋Š” secret_key ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋”ฐ๋กœ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค.

      $ touch my_settings.py

    • my_settings.py์— ์‹ค์ œ ์“ฐ์—ฌ์ง€๋Š” ๋‚ด์šฉ

      DATABASES = {
      'default' : {
      'ENGINE': 'django.db.backends.mysql',
      'NAME': 'DATABASE ๋ช…',
      'USER': 'DB์ ‘์† ๊ณ„์ •๋ช…',
      'PASSWORD': 'DB์ ‘์†์šฉ ๋น„๋ฐ€๋ฒˆํ˜ธ',
      'HOST': '127.0.0.1',
      'PORT': '3306',
      'OPTIONS': {'charset': 'utf8mb4'}
      }
      }

      SECRET_KEY = '์‹œํฌ๋ฆฟํ‚ค'
      #settings.py์— ์žˆ๋Š” secret_key ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

    • setting.py ์™€ my_settings.py ์—ฐ๋™

      from pathlib import Path #๊ธฐ์กด์— settings.py ์— ์žˆ๋Š” ์ฝ”๋“œ
      from my_settings import DATABASES, SECRET_KEY
      ...
      DATABASES = DATABASES
      SECRET_KEY = SECRET_KEY

  • corsheaders

    • CORS๋Š” Cross Origin Resource Sharing์˜ ์•ฝ์ž์ด๋ฉฐ, corsheaders๋Š” Cross Domain ์ด์Šˆ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์„œ๋ฒ„์˜ ํ—ค๋”๋ฅผ ์กฐ์ž‘ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ํŒจํ‚ค์ง€์ด๋‹ค.

      ๐Ÿ‘‰ django-cors-headers๋ฅผ ์„ค์น˜ํ•œ๋‹ค.
      $ pip install django-cors-headers

      ๐Ÿ‘‰ settings.py ์„ค์ •

      INSTALLED_APPS = [
      ...
      'corsheaders'
      ]

      MIDDLEWARE = [
      ...
      'corsheaders.middleware.CorsMiddleware',
      ...
      ]

      ๐Ÿ‘‰ CORS ์ถ”๊ฐ€์„ค์ • : settings.py ์ œ์ผ ์•„๋žซ๋ถ€๋ถ„์— ์•„๋ž˜์˜ ์ฝ”๋“œ ์ถ”๊ฐ€
      ##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',
      )

  • ์„œ๋ฒ„ ๊ตฌ๋™ ํ…Œ์ŠคํŠธ

    $ python manage.py runserver

  • .gitignore ์ƒ์„ฑ

    • https://www.toptal.com/developers/gitignore ์ ‘์†ํ•˜์—ฌ,
      ์•„๋ž˜์˜ ํ‚ค์›Œ๋“œ ์ถ”๊ฐ€ ํ•œํ›„ Create!

      python, pycharm, VisualStudioCode, vim, macOS, Linux, zsh

    • create ํ•˜์—ฌ ๋‚˜์˜จ ๋‚ด์šฉ .gitignore ํŒŒ์ผ ์ƒ์„ฑ ํ›„ ํŒŒ์ผ์˜ ๋‚ด์šฉ์œผ๋กœ ๋ถ™์—ฌ๋„ฃ๊ธฐ (.gitignore์˜ ์œ„์น˜๋Š” manage.py, my_settings.py ์™€ ๋™์ผํ•œ ์œ„์น˜์— ์ƒ์„ฑํ•œ๋‹ค.)

    • ๋ถ™์—ฌ๋„ฃ๊ธฐ ํ•œ ๋‚ด์šฉ ์ œ์ผ ์•„๋žซ ๋ถ€๋ถ„์— "my_settings.py" ์ถ”๊ฐ€ํ•œ๋‹ค.

profile
Backend Developer - "Growth itself contains the germ of happiness"
post-custom-banner

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