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
Was this helpful?