Start Django Project

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

$ django-admin startproject <project_name>

์ด๋•Œ project๋ช…์œผ๋กœ ์„ค์ • ํŒจํ‚ค์ง€ ๋ช…์ด ๊ฒฐ์ •๋˜๋ฏ€๋กœ startproject์— ์ „๋‹ฌ๋˜๋Š” ํ”„๋กœ์ ํŠธ๋ช…์œผ๋กœ config๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋‹ค.

$ django-admin startproject config

ํ”„๋กœ์ ํŠธ ์ƒ์„ฑํ›„์—๋Š” ํด๋”๋ช…์„ ํ”„๋กœ์ ํŠธ ๋ช…์œผ๋กœ ๋ณ€๊ฒฝํ•ด์ค€๋‹ค.

$ mv config <project_name>

๊ธฐ๋ณธ ๋””๋ ‰ํ† ๋ฆฌ ๋ฐ ํŒŒ์ผ ๊ตฌ์กฐ

ํ˜„์žฌ ๋””๋ ‰ํ† ๋ฆฌ์— <project_name> ๋””๋ ‰ํ† ๋ฆฌ์™€ ์•„๋ž˜์˜ ํŒŒ์ผ๋“ค์ด ์ƒ์„ฑ๋œ๋‹ค.

project_name/
โ”œโ”€โ”€ db.sqlite3
โ”œโ”€โ”€ manage.py
โ””โ”€โ”€ config
    โ”œโ”€โ”€ __init__.py
    โ”œโ”€โ”€ settings.py
    โ”œโ”€โ”€ urls.py
    โ””โ”€โ”€ wsgi.py
  • manage.py : ์‚ฌ์ดํŠธ ๊ด€๋ฆฌ๋ฅผ ๋„์™€์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๋Š” ์Šคํฌ๋ฆฝํŠธ์ด๋‹ค. ๋‹ค๋ฅธ ์ปดํ“จํ„ฐ์—์„œ ๋‹ค๋ฅธ ์„ค์น˜ ์ž‘์—…์—†์ด ์›น ์„œ๋ฒ„๋ฅผ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค.(์ถ”ํ›„์— ๊ด€๋ จ ์ •๋ณด update)

  • config/ : ๋””๋ ‰ํ† ๋ฆฌ ๋‚ด๋ถ€์— ํ”„๋กœ์ ํŠธ๋ฅผ ์œ„ํ•œ ์‹ค์ œ python ํŒจํ‚ค์ง€๋“ค์ด ์ €์žฅ๋œ๋‹ค.

    • config.urls , config.settings ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ”„๋กœ์ ํŠธ ๋‚ด๋ถ€ ์–ด๋””์—์„œ๋‚˜ ์ ‘๊ทผ(import)ํ•  ์ˆ˜ ์žˆ๋‹ค.

  • config/__init__.py : ํ•ด๋‹น ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ํŒจํ‚ค์ง€์˜ ์ผ๋ถ€์ž„์„ ์•Œ๋ ค์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

python version 3.3 ๋ถ€ํ„ฐ๋Š” __init__.py ๊ฐ€ ์—†์–ด๋„ ํŒจํ‚ค์ง€๋กœ ์ธ์‹์ด๋˜์ง€๋งŒ, ํ•˜์œ„ ๋ฒ„์ „๊ณผ ํ˜ธํ™˜์„ ์œ„ํ•ด ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด ์•ˆ์ „ํ•˜๋‹ค.

  • config/settings.py : ํ˜„์žฌ Django ํ”„๋กœ์ ํŠธ์˜ ํ™˜๊ฒฝ/๊ตฌ์„ฑ์„ ์ €์žฅํ•œ๋‹ค.(์ถ”ํ›„์— ๊ด€๋ จ ์ •๋ณด update)

  • config/urls.py : ํ˜„์žฌ Django ํ”„๋กœ์ ํŠธ์˜ URL์„ ์–ธ์„ ์ €์žฅํ•œ๋‹ค. urlresolver๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ํŒจํ„ด ๋ชฉ๋ก์„ ํฌํ•จํ•œ๋‹ค.(์ถ”ํ›„์— ๊ด€๋ จ ์ •๋ณด update)

  • config/wsgi.py : ํ˜„์žฌ ํ”„๋กœ์ ํŠธ๋ฅผ ์„œ๋น„์Šคํ•˜๊ธฐ ์œ„ํ•œ WSGI config ์„ค์ •ํŒŒ์ผ์ด๋‹ค.

WSGI(Web Server Gateway Interface)

์›น์„œ๋ฒ„์™€ ํŒŒ์ด์ฌ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐœ๋ฐœํ™˜๊ฒฝ ๊ฐ„์˜ ์ธํ„ฐํŽ˜์ด์Šค์— ๋Œ€ํ•œ ๊ทœ์น™์ด๋‹ค.

์—ฌ๊ธฐ์„œ ์„ค์น˜๋œ pip ํŒจํ‚ค์ง€ ๋ชฉ๋ก์„ requirements.txt ๋กœ ๋‚จ๊ฒจ๋‘์–ด ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

$ pip freeze > requirements.txt
project
โ”œโ”€โ”€ project_name
โ”‚   โ”œโ”€โ”€ config                                                        # ์žฅ๊ณ ํ”„๋กœ์ ํŠธ ์„ค์ • ํŒจํ‚ค์ง€
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”‚   โ”œโ”€โ”€ settings.py
โ”‚   โ”‚   โ”œโ”€โ”€ urls.py
โ”‚   โ”‚   โ””โ”€โ”€ wsgi.py
โ”‚   โ”œโ”€โ”€ db.sqlite3
โ”‚   โ””โ”€โ”€ manage.py
โ””โ”€โ”€ requirements.txt                                                # ์„ค์น˜ํ•  pip ํŒจํ‚ค์ง€ ๋ชฉ๋ก ํŒŒ์ผ

ํ”„๋กœ์ ํŠธ ์„œ๋ฒ„

์„œ๋ฒ„ ์‹œ์ž‘ํ•˜๊ธฐ

$ python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
March 05, 2019 - 16:21:39
Django version 2.1.7, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

runserver๋ฅผ ์„ฑ๊ณตํ•˜๋ฉด http://localhost:8000/ ๋˜๋Š” http://127.0.0.1:8000/ ๋กœ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

ํŠน์ • ํฌํŠธ๋กœ ์„œ๋ฒ„ ์‹œ์ž‘ํ•˜๊ธฐ

$ python manage.py runserver 8080

์„œ๋ฒ„ ์‹คํ–‰ ์ข…๋ฃŒํ•˜๊ธฐ

control + C ๋ฅผ ํ•˜๋ฉด ์ข…๋ฃŒ๋œ๋‹ค.

ํ”„๋กœ์ ํŠธ ์„ค์ •(settings.py)

Timezone ์„ค์ •

์ •ํ™•ํ•œ ์‹œ๊ฐ„ ์„ค์ •์„ ์œ„ํ•ด์„œ wiki timezone ์—์„œ ์ฐพ์•„์„œ ์„ค์ •ํ•ด์ค€๋‹ค.

LANGUAGE_CODE = 'ko'
TIME_ZONE = 'Asia/Seoul'

STATIC_ROOT ์„ค์ •

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

STATIC_URL ์„ค์ • ๋ฐ‘์— ๊ฒฝ๋กœ๋ฅผ ์„ค์ •ํ•ด์ค€๋‹ค.

ALLOWED_HOSTS

DEBUG = True
ALLOWED_HOSTS = []

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค์ •์ด ๋˜์–ด์žˆ์„๋•Œ๋Š” ['localhost', '127.0.0.1', '[::1]'] ์— ๋Œ€ํ•ด์„œ ์œ ํšจํ•˜๋‹ค.

INSTALLED_APPS

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
  • django.contrib.admin : ๊ด€๋ฆฌ์šฉ ์‚ฌ์ดํŠธ

  • django.contrib.auth : ์ธ์ฆ ์‹œ์Šคํ…œ

  • django.contrib.contenttypes : ์ปจํ…์ธ  ํƒ€์ž…์„ ์œ„ํ•œ ํ”„๋ ˆ์ž„ ์›Œํฌ

  • django.contrib.sessions : ์„ธ์…˜ ํ”„๋ ˆ์ž„์›Œํฌ

  • django.contrib.messages : messaging ํ”„๋ ˆ์ž„์›Œํฌ

  • django.contrib.staticfiles : ์ •์ ํŒŒ์ผ์„ ๊ด€๋ฆฌํ•˜๋Š” ํ”„๋ ˆ์ž„์›Œํฌ

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค

migration

migrate ๋ช…๋ น์€ settings.py ์˜ INSTALLED_APPS ์„ค์ •์„ ํƒ์ƒ‰ํ•˜์—ฌ, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค migrations์— ๋”ฐ๋ผ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•œ๋‹ค.

$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying sessions.0001_initial... OK

migrateํ•˜๊ธฐ ์ „์— ๋ถˆํ•„์š”ํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜(INSTALLED_APPS)์€ ์ฃผ์„์ฒ˜๋ฆฌ ๋˜๋Š” ์‚ญ์ œํ•ด์ฃผ๋ฉด ์ƒ์„ฑ๋˜์ง€์•Š๋Š”๋‹ค.

ModuleNotFoundError

....
  File "/Users/jeongdaye/.pyenv/versions/3.7.2/lib/python3.7/sqlite3/dbapi2.py", line 27, in <module>
    from _sqlite3 import *
ModuleNotFoundError: No module named '_sqlite3'

Migrate ์‹คํ–‰์‹œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ์—๋Š”

$ CFLAGS="-I$(xcrun --show-sdk-path)/usr/include" pyenv install <version>
$ CFLAGS="-I$(xcrun --show-sdk-path)/usr/include" pyenv install 3.7.2

์ž๊ธฐ๊ฐ€ ์„ค์ •ํ•œ python version์œผ๋กœ ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

git ๋ฒ„์ „ ๊ด€๋ฆฌ

git์œผ๋กœ ๋ฒ„์ „ ๊ด€๋ฆฌ์‹œ git์œผ๋กœ ๊ด€๋ฆฌ๋˜์ง€ ์•Š์„ ํŒŒ์ผ ๋ชฉ๋ก์ธ .gitignore ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด ๊ด€๋ฆฌํ•œ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ Django ํ”„๋กœ์ ํŠธ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ํŒŒ์ผ์„ wget์œผ๋กœ ์‚ฌ์šฉํ•ด ์›น์—์„œ ๋ณต์‚ฌํ•ด์™€ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋‹ค.

$ wget https://gist.githubusercontent.com/LeeHanYeong/8758517113be32dd2e885fef81c4a96e/raw/00727ac4af42834e6282df05c61606aa396d5b9c/.gitignore

์ฐธ์กฐํŽ˜์ด์ง€

Last updated