Notice
Recent Posts
Recent Comments
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Tags more
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

๐ŸŒฒ์ž๋ผ๋‚˜๋Š”์ฒญ๋…„

[์žฅ๊ณ ] ์ฒซ ๋ฒˆ์งธ ์žฅ๊ณ  ์•ฑ ์ž‘์„ฑํ•˜๊ธฐ ํŠœํ† ๋ฆฌ์–ผ 1 ๋ณธ๋ฌธ

django

[์žฅ๊ณ ] ์ฒซ ๋ฒˆ์งธ ์žฅ๊ณ  ์•ฑ ์ž‘์„ฑํ•˜๊ธฐ ํŠœํ† ๋ฆฌ์–ผ 1

JihyunLee 2019. 9. 17. 15:08
๋ฐ˜์‘ํ˜•

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

 

์ฒซ ๋ฒˆ์งธ ์žฅ๊ณ  ์•ฑ ์ž‘์„ฑํ•˜๊ธฐ, part 1 | Django ๋ฌธ์„œ | Django

Django The web framework for perfectionists with deadlines. Overview Download Documentation News Community Code Issues About ♥ Donate

docs.djangoproject.com

1. ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค๊ณ  ์‹คํ–‰์‹œ์ผœ ๋ณด๊ธฐ

(python ๊ณผ django ๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ๋‹ค๋Š” ์ „์ œ ํ•˜์—)

1
django-admin startproject mysite
cd mysite
python manage.py runserver
cs

์œ„ ์ฝ”๋“œ ์„ธ์ค„์„ ์‹คํ–‰์‹œํ‚ค๋ฉด, ๋กœ์ผ“๋ชจ์–‘์˜ django ์ฒซ ํŽ˜์ด์ง€๊ฐ€ ๋œจ๊ฒŒ ๋œ๋‹ค.

 

2. ์„ค๋ฌธ์กฐ์‚ฌ ์•ฑ ๋งŒ๋“ค๊ธฐ

์•ฑ์€ ํ”„๋กœ์ ํŠธ ๋‚ด๋ถ€์— ์„ธ๋ถ€์ ์ธ ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ๋Š” ์„ค๋ฌธ์กฐ์‚ฌ ์•ฑ์„ ๋งŒ๋“ค์—ˆ๋‹ค.

 

์•ฑ์„ ๋งŒ๋“œ๋Š” ๋ช…๋ น์–ด๋Š” 

1
python manage.py startapp polls
cs

์ด๋‹ค. polls ๋ผ๋Š” ํด๋”๊ฐ€ ์ƒ๊ธด๋‹ค.

 

3. ๋ทฐ ์ž‘์„ฑํ•˜๊ธฐ

์žฅ๊ณ ๋Š” view.py๋ฅผ ์ž˜ ์‚ฌ์šฉํ•˜๋Š”๋ฐ ๊ธฐ๋Šฅ์€ url ์š”์ฒญ์ด ๋“ค์–ด์™”์„ ๋•Œ, ์–ด๋–ค ํ™”๋ฉด์„ ๋ณด์—ฌ์ค„์ง€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์—ฌ๊ธฐ์— ๋‘”๋‹ค.

polls/view.py ๋ฅผ  ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ–ˆ๋‹ค.

 

1
2
3
4
5
from django.http import HttpResponse
 
 
def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")
cs

 

์œ„์˜ ์ฝ”๋“œ๋Š” /polls๋ผ๋Š” ์š”์ฒญ์ด ๋“ค์œผ์˜ค๋ฉด, Hello, world. You're at the polls index. ๋ฌธ์ž๋ฅผ ํ™”๋ฉด์— ๋„์›Œ์ฃผ๋Š” ์ฝ”๋“œ์ด๋‹ค.

url ์— ๋”ฐ๋ผ ์œ„ ๋ฌธ์ž๋ฅผ ํ™”๋ฉด์— ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•ด์„œ๋Š” /polls๋ผ๋Š” url๊ณผ index(request)ํ•จ์ˆ˜๋ฅผ ์—ฐ๊ฒฐ์‹œ์ผœ ์ฃผ์–ด์•ผ ํ•˜๋Š”๋ฐ, url๊ณผ view์˜ ํ•จ์ˆ˜(๋‚˜์ค‘์—๋Š” class๋„)๋ฅผ ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” ์ฝ”๋“œ๋Š” url.py์— ์žˆ๋‹ค. 

 

url.py๋Š” mysite/url.py๊ฐ€ ์žˆ๊ณ  polls/url.py ๊ฐ€ ์žˆ๋Š”๋ฐ ๋‘˜ ๋‹ค๋ฅผ ์ˆ˜์ •ํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

localhost:8080/polls/~~/~~/ ๋ผ๋Š” ์š”์ฒญ์ด ๋“ค์–ด์™”์„ ๋•Œ ์ด๋ฅผ localhost:8080/polls ๋ถ„์€ mysite/url.py๊ฐ€ ์ฒ˜๋ฆฌํ•˜๊ณ , ๋‚˜๋จธ์ง€ ~~/~~/ ๋ถ€๋ถ„์€ polls/url.py๊ฐ€ ์ฒ˜๋ฆฌํ•˜๋Š”๊ฒŒ ์ผ๋ฐ˜์ ์ด๋‹ค.

 

๊ทธ๋ ‡๊ฒŒ ํ•˜๋ ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋‘ ํŒŒ์ผ์˜ ์ฝ”๋“œ๋ฅผ ๋ฐ”๊ฟ”์•ผ ํ•œ๋‹ค.

- mysite.urls ์—์„œ polls๊นŒ์ง€์˜ url๋งŒ ์ฒ˜๋ฆฌํ•˜๊ณ  ๋‚˜๋จธ์ง€ url์€ polls.urls๊ฐ€ ์ฒ˜๋ฆฌํ•˜๋„๋ก ๋„˜๊ฒจ์ฃผ๋Š” ์ฝ”๋“œ

- include() ํ•จ์ˆ˜๋Š” ๋‹ค๋ฅธ url๊ทœ์น™์„ ์ฐธ์กฐํ•˜๋ผ๋Š” ๋œป

1
2
3
4
5
6
7
from django.contrib import admin
from django.urls import include, path
 
urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
]
cs

- ๋„˜๊ฒจ๋ฐ›์€ url์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์ฝ”๋“œ. ์ง€๊ธˆ ' ' ์˜ ์˜๋ฏธ๋Š” localhost:8080/polls ์ด์™ธ์— ๋’ท๋ถ€๋ถ„์€ ์—†๋‹ค๋Š” ๋œป.

1
2
3
4
5
6
7
from django.urls import path
 
from . import views
 
urlpatterns = [
    path('', views.index, name='index'),
]
cs

 

 

๋ฐ˜์‘ํ˜•