[TIL - 14 / Django] Django Tutorial

haejun-kimยท2020๋…„ 7์›” 31์ผ
0

[Django]

๋ชฉ๋ก ๋ณด๊ธฐ
12/20

Django ๊ณต์‹ ๋ฌธ์„œ

๐Ÿ™„๊ณต์‹๋ฌธ์„œ๋ฅผ ๋ณด๋ฉด์„œ ๊ฐœ๋…์— ๋Œ€ํ•œ ์ •๋ฆฌ๋ฅผ ํ•œ ํŽ˜์ด์ง€. ์žฅ๊ณ ๋ฅผ ์ฒ˜์Œ ๋ฐฐ์šฐ๋Š” ์ž…์žฅ์—์„œ ๊ทธ์ € ์ฝ”๋“œ๋ฅผ ๋ฌด์˜์‹์ ์œผ๋กœ ๋”ฐ๋ผ์น˜๊ธฐ๋ณด๋‹ค๋Š” ์›น์ด ์–ด๋–ค ํ๋ฆ„์œผ๋กœ ์ด๋ฃจ์–ด์ง€๋Š”๊ฐ€๋ฅผ ์ดํ•ดํ•˜๋Š”๊ฒƒ์ด ๋” ์ค‘์š”ํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”๋“œ์— ๋Œ€ํ•œ ๋‚ด์šฉ์€ ๋ธ”๋กœ๊น…ํ•˜์ง€ ์•Š์•˜๋‹ค. (๊ทธ๋ ‡๋‹ค๊ณ  ์ฝ”๋“œ๋ฅผ ์•ˆ์ณ๋ณธ๊ฑด ์•„๋‹ˆ๋‹ค. ๋‹ค ๋”ฐ๋ผ์ณ๋ดค๋‹ค!)


What is MTV Pattern

์žฅ๊ณ ๋Š” MTV ํŒจํ„ด์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ํ”„๋ ˆ์ž„์›Œํฌ๋ผ๋Š” ๋ง์„ ๊ฒ€์ƒ‰์„ ํ•˜๋ฉด์„œ ๊ต‰์žฅํžˆ ๋งŽ์ด ๋ณด์•˜๋‹ค. ๊ทธ๋ž˜์„œ ์ด MTV ํŒจํ„ด์ด ๋ฌด์—‡์ธ์ง€์— ๋Œ€ํ•œ ์ดํ•ด๊ฐ€ ์žˆ์–ด์•ผ ์žฅ๊ณ ๋ผ๋Š” ์›น ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์„๊ฑฐ๋ž€ ์ƒ๊ฐ์ด ๋“ค์–ด์„œ ์ •๋ฆฌํ•ด๋†“์œผ๋ คํ•œ๋‹ค.

๊ทธ๋ž˜์„œ MTVํŒจํ„ด์ด ๋ญ”๋ฐ? MTVํŒจํ„ด์„ ์ด์•ผ๊ธฐํ•˜๊ธฐ ์ „์— MVCํŒจํ„ด์œผ๋กœ ๋จผ์ € ์ดํ•ดํ•ด๋ณด์ž. MVC ํŒจํ„ด์€ Model, View, Control ๋กœ ๋™์ž‘ํ•˜๋Š” ํŒจํ„ด์„ ์˜๋ฏธํ•œ๋‹ค. ์ด ํŒจํ„ด์ด ์žฅ๊ณ ์—์„œ๋Š” Control ์„ Template์œผ๋กœ ๋ฐ”๊ฟ” MTV ํŒจํ„ด์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ์ด๋ฆ„๋งŒ ๋‹ค๋ฅด์ง€ ํ•˜๋Š” ์—ญํ• ์€ ๊ฑฐ์˜ ๋น„์Šทํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ์กด์— MVC ํŒจํ„ด์„ ์ดํ•ดํ•˜๊ณ  ์žˆ๋‹ค๋ฉด ์ดํ•ดํ•˜๊ธฐ ์กฐ๊ธˆ ๋” ์ˆ˜์›”ํ• ๊ฑฐ๋ผ ์ƒ๊ฐํ•œ๋‹ค.

MVC ํŒจํ„ด์„ ์ดํ•ดํ•˜๊ธฐ์— ์ข‹์€ ๊ทธ๋ฆผ์ด ์žˆ์–ด ๋‹ค์Œ์˜ ํŽ˜์ด์ง€์—์„œ ๊ฐ€์ง€๊ณ ์™”๋‹ค. ์˜์–ด์— ์ž์‹ ์ด ์žˆ๋‹ค๋ฉด ์•„๋ž˜์˜ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•˜๋ฉด ์ดํ•ดํ•˜๋Š”๋ฐ ๋„์›€์ด ๋ ๊ฑฐ๋ผ ์ƒ๊ฐํ•œ๋‹ค.

Essence of MVC

๊ธฐ์กด์— MVC๋Š” ๋ณ„๊ฐœ์˜ ๊ฐœ๋…์ด ์•„๋‹Œ ํ•˜๋‚˜์˜ ํ†ตํ•ฉ๋œ ๊ฐœ๋…์ด์˜€๋‹ค. ์ด ๊ฐœ๋…์ด ์„ž์—ฌ์žˆ์–ด ํ”„๋กœ์ ํŠธ๊ฐ€ ์ปค์ ธ๊ฐ์— ๋”ฐ๋ผ ๊ด€๋ฆฌ์— ์–ด๋ ค์›€์„ ๋Š๊ปด MVC๋ฅผ ๋”ฐ๋กœ ๊ด€๋ฆฌํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.

MVC๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์—ญํ• ์„ ํ•œ๋‹ค.

MVC concerns by function:

  • Model:ย safely and consistently store the data that matters to the user
  • View:ย present all, a subset or summarization of the data to the user
  • Controller:ย respond to user inputs and events, update the view and model appropriately

MVC concerns by expertise:

  • Model:ย data reliability, data consistency, query efficiency, update efficiency and storage-size efficiency
  • View:ย graphic design, information presentation and UX
  • Controller:ย expertise in the problem domain and coordinating the other concerns

MVC concerns by people:

  • Model:ย algorithm and data-engineers
  • View:ย designers and artists
  • Controller:ย experts in what the system needs to do

์ฆ‰, ์ •๋ฆฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • Model : ์•ˆ์ „ํ•˜๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ
  • View : ๋ฐ์ดํ„ฐ๋ฅผ ์ ์ ˆํ•˜๊ฒŒ ์œ ์ €์—๊ฒŒ ๋ณด์—ฌ์คŒ
  • Control ( Template ) : ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ๊ณผ ์ด๋ฒคํŠธ์— ๋ฐ˜์‘ํ•˜์—ฌ Model๊ณผ View๋ฅผ ์—…๋ฐ์ดํŠธ

์ด๋ ‡๊ฒŒ ๋ถ„๋ฆฌ ๋œ MVC๋Š” ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ• ๊นŒ? ๋‹ค์Œ ๊ทธ๋ฆผ์„ ๋ด๋ณด์ž.

์—ฌ๊ธฐ์„œ controller๋ฅผ MTVํŒจํ„ด์—์„œ๋Š” Template๋กœ ์ƒ๊ฐํ•˜๋ฉด ๋˜‘๊ฐ™๋‹ค! ์ด๋Ÿฐ ํŒจํ„ด์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋™์ž‘ํ•œ๋‹ค.

  1. User โ†’ Controller์—์„œ ๋‹ค์–‘ํ•œ Input, event ์กฐ์ž‘ โ†’ Controller๋Š” Model์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์™€์„œ ๊ฐ€๊ณต ํ›„ View์— ๋ณด๋‚ด์ง€๊ณ , View๋ฅผ ๋‹ค์‹œ ์œ ์ €์—๊ฒŒ ๋ณด์—ฌ์ง€๋Š” ํ˜•ํƒœ
  2. user๊ฐ€ ์ž…๋ ฅํ•˜๋ฉด Controller๊ฐ€ ์ž…๋ ฅ์„ Model์— ์“ฐ๋Š” ํ˜•ํƒœ

๋”ฐ๋ผ์„œ ๋ชจ๋‘ ์„œ๋กœ ์œ ๊ธฐ์ ์ธ ๊ด€๊ณ„๋กœ ๋™์ž‘ํ•œ๋‹ค!


Django Flow

๊ทธ๋ž˜ MTV ํŒจํ„ด์€ ๊ทธ๋Ÿฐ๊ฑฐ๊ตฌ๋‚˜. ๊ทธ๊ฒŒ Django์—์„œ ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๋Š”๋ฐ? ๋ผ๋Š” ๋‚ด ์Šค์Šค๋กœ์˜ ์งˆ๋ฌธ์— ๋Œ€๋‹ตํžˆ๊ธฐ ์œ„ํ•ด ์ฐพ์•„๋ณธ Django Flow! ์žฅ๊ณ ์˜ ํ๋ฆ„์„ ์•Œ์•„๋ณด์ž.


์œ„์˜ ๊ทธ๋ฆผ์„ ๋ณด๋ฉด์„œ ๋‹ค์Œ์˜ ํ๋ฆ„์„ ์ดํ•ดํ•ด๋ณด์ž.

  1. event ๋ฐœ์ƒ
  2. ์š”์ฒญํ•œ URL ๋ถ„์„. ์ ํ•ฉํ•œ View๋กœ ๋ณด์—ฌ์คŒ
  3. Database ์–ด๋””์— ์ ‘๊ทผํ•ด์„œ ์–ด๋–ค ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€๊ณตํ• ์ง€ Model์— ์•Œ๋ ค์ค€๋‹ค.
  4. ์‹ค์ œ์ ์ธ Database์™€ connection
  5. Data send ์ค€๋น„
  6. ์‹ค์ œ Data send
  7. User์—๊ฒŒ ๋ณด์—ฌ์ค„ Data๋ฅผ Template์— ์ „์†ก
  8. ๋‹ค์–‘ํ•œ UI๋ฅผ ๋งŒ๋“ค์–ด์„œ browswer์— send
  9. 1์—์„œ ๋ฐœ์ƒํ•œ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ ์™„๋ฃŒ!

์œ„์™€ ๊ฐ™์€ ๋ฃจํ”„๋ฅผ ์ง€์†์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋ฉด์„œ ์žฅ๊ณ ๋Š” ๋™์ž‘ํ•œ๋‹ค. ์ค‘์š”ํ•œ ๋ถ€๋ถ„์ด๋‹ˆ ๊ผญ ํ๋ฆ„์„ ์ดํ•ดํ•˜๋„๋ก ํ•˜์ž.


์žฅ๊ณ ์˜ MTV ํŒจํ„ด๊ณผ ์žฅ๊ณ ์˜ ํ๋ฆ„์„ ์ดํ•ดํ–ˆ๋‹ค๋ฉด ์ด์ œ ์‹ค์ œ๋กœ ์žฅ๊ณ ๋ฅผ ์จ๋ด์•ผํ•œ๋‹ค. ์ด ํŽ˜์ด์ง€์—์„œ๋Š” ์žฅ๊ณ ์—์„œ ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค๊ณ  ์•ฑ์„ ๋งŒ๋“ค๊ฒƒ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋งŒ๋“ค๋ฉด์„œ ์ƒ์„ฑ๋œ ํŒŒ์ผ๋“ค์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•˜๋Š”๊ฑธ๋กœ ๋๋งˆ์น˜๊ฒ ๋‹ค.

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

$ django-admin startproject mystie

mysite๋ผ๋Š” ํ”„๋กœ์ ํŠธ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ํ”„๋กœ์ ํŠธ๋Š” ํ•˜๋‚˜์˜ ํ”„๋กœ์ ํŠธ๋ฅผ ์ด์•ผ๊ธฐํ•˜๋ฉฐ ์•ฑ์€ ํ•˜๋‚˜์˜ ํ”„๋กœ์ ํŠธ ์•ˆ์— ์žˆ๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๊ธฐ๋Šฅ๋“ค์„ ์•ฑ์ด๋ผ๊ณ  ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด django tutorialํŽ˜์ด์ง€์—์„œ๋Š” mysite๋ผ๋Š” ํ”„๋กœ์ ํŠธ ์•ˆ์— ํˆฌํ‘œ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์ด ์žˆ๋Š” ์•ฑ์ธ polls ๋ผ๋Š” ์•ฑ์„ ๋งŒ๋“œ๋Š” ์˜ˆ์ œ๊ฐ€ ์žˆ๋‹ค.

ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ, Python ๋˜๋Š” Django์—์„œ ์‚ฌ์šฉ์ค‘์ธ ์ด๋ฆ„์€ ์ถฉ๋Œ์ด ์ผ์–ด๋‚˜๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉ์„ ํ”ผํ•˜์ž.

startproject ๋ฅผ ํ•˜๊ณ  ๋‚˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŒŒ์ผ๋“ค์ด ์ƒ์„ฑ๋œ๋‹ค.

์ด๋Ÿฌํ•œ ํŒŒ์ผ๋“ค์˜ ๊ตฌ์„ฑ๊ณผ ์—ญํ• ๋“ค์— ๋Œ€ํ•ด์„œ ์ดํ•ดํ•˜๋Š”๊ฒŒ ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค.

๊ตฌ์„ฑ์„ ์‚ดํŽด๋ณด๋ฉด ๋‚ด๊ฐ€ ์ƒ์„ฑํ•œ mysite๋ผ๋Š” ํ”„๋กœ์ ํŠธ๊ฐ€ ์ƒ์„ฑ์ด ๋˜์—ˆ๊ณ , manage.py๋ผ๋Š” ํŒŒ์ผ์ด ์ƒ์„ฑ๋˜์—ˆ๋‹ค.

  • manage.py : Django ํ”„๋กœ์ ํŠธ์™€ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ์ƒํ˜ธ์ž‘์šฉํ•˜๋Š” ์ปค๋งจ๋“œ๋ผ์ธ์˜ ์œ ํ‹ธ๋ฆฌํ‹ฐ. ์ž„์˜๋กœ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๋„๋ก ์ฃผ์˜
  • mystie : ์ด ๋””๋ ‰ํ† ๋ฆฌ ๋‚ด๋ถ€์—๋Š” ํ”„๋กœ์ ํŠธ๋ฅผ ์œ„ํ•œ ์‹ค์ œ python ํŒจํ‚ค์ง€ ํŒŒ์ผ๋“ค์ด ์ €์žฅ๋œ๋‹ค.
  • __init__.py : Python์œผ๋กœ ํ•˜์—ฌ๊ธˆ ์ด ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ํŒจํ‚ค์ง€์ฒ˜๋Ÿผ ๋‹ค๋ฃจ๋ผ๊ณ  ์•Œ๋ ค์ฃผ๋Š” ์šฉ๋„์˜ ๋นˆ ํŒŒ์ผ
  • settings.py: ์ „์ฒด ํ”„๋กœ์ ํŠธ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์„ค์ •๋“ค์ด ๋‹ด๊ฒจ์žˆ๋Š” ํŒŒ
  • urls.py: ์‚ฌ์šฉ์ž๊ฐ€ URL๋กœ Django์— ์ ‘๊ทผ์„ ํ•˜๋ฉด Django๋Š” URL๋กœ URL ๊ทœ์น™์„ ๋ณด๊ณ  ๋‚ด๋ถ€์—์„œ ์ผ์น˜ํ•˜๋Š” view๋ฅผ ์ฐพ์•„ ์—ฐ๊ฒฐ์‹œ์ผœ์ค€๋‹ค.
  • asgi.py: asgi๋Š” Django-channels๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์•Œ์•„์•ผ ํ•˜๋Š” ๊ฐœ๋…. Asynchronous Server Gateway Interface ์˜ ์ค„์ž„๋ง๋กœ์จ, django-channels๊ฐ€ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” Daphne์™€ django-channels๊ฐ€ ์ž‘๋™ํ•˜๋Š” ๊ธฐ๋ฐ˜.
  • wsgi.py: Web Server Gateway Interface. Python์˜ ํ‘œ์ค€ Gateway Interface.

๊ฐœ๋ฐœ ์„œ๋ฒ„

python manage.py runserver

์ƒ๋‹จ์˜ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ–ˆ์„ ๋•Œ, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ช…๋ น์–ด๊ฐ€ ์ถœ๋ ฅ๋˜๋ฉด ์ •์ƒ์œผ๋กœ ๋™์ž‘ํ•œ๊ฒƒ์ด๋‹ค.

Performing system checks...
System check identified no issues (0 silenced).
You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
7์›” 29, 2020 - 15:50:53
Django version 3.0, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

๋นจ๊ฐ„์ค„๋กœ ํ‘œ์‹œ๋˜์–ด ๋‚˜์˜ค๋Š” ๊ฒฝ๊ณ ๋ฌธ๊ตฌ๋Š” ํ˜„์žฌ๋Š” ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š์•„๋„ ๋œ๋‹ค. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ์šฉ๋˜์ง€ ์•Š์€ ๋ณ€๊ฒฝ์‚ฌํ•ญ๋“ค(migrations)์— ๋Œ€ํ•œ ๊ฒฝ๊ณ ์ด๋‹ค.

์•ฑ ๋งŒ๋“ค๊ธฐ

python manage.py startapp polls

์ด์ œ mysite๋ผ๋Š” ํ”„๋กœ์ ํŠธ์—์„œ polls ๋ผ๋Š” ์•ฑ์„ ์ƒ์„ฑํ–ˆ๋‹ค. ํ˜„์žฌ ํด๋”์˜ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

๊ฐ ํŒŒ์ผ๋“ค์˜ ๊ธฐ๋Šฅ์„ ์•Œ์•„์•ผ ๋‚ด๊ฐ€ ์‹ค์ œ ๊ฐœ๋ฐœํ•  ๋•Œ ์–ด๋–ค ํŒŒ์ผ์— ์–ด๋–ค ๋กœ์ง์„ ๊ตฌํ˜„ํ•ด์•ผํ• ์ง€ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ ํŒŒ์ผ๋“ค์ด ์–ด๋–ค ํŒŒ์ผ์ธ์ง€ ๊ฐ„๋‹จํžˆ ์•Œ์•„๋ณด์ž.

  • __init__.py: App์ด๋ผ๊ณ  ์•Œ๋ ค์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๋Š” ํŒŒ์ผ
  • admin.py: Django์—์„œ๋Š” admin์ด๋ผ๊ณ  ํ•˜๋Š” ๊ด€๋ฆฌ์ž ํŽ˜์ด์ง€๋„ ์ œ๊ณตํ•ด์ฃผ๋Š”๋ฐ, ์ด ๊ด€๋ฆฌ์ž์— ๋Œ€ํ•œ ๋กœ์ง์„ ๊ตฌํ˜„ํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ํŒŒ์ผ
  • migrations: ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ๊ด€๋ จ๋œ ํด๋”
  • models.py: ๋ชจ๋ธ๋ง์— ์‚ฌ์šฉ๋  ๋กœ์ง๋“ค์„ ๊ตฌํ˜„ํ•˜๋Š” ํŒŒ์ผ. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์—ฐ๊ฒฐ์„ ์ง์ ‘์ ์œผ๋กœ ๋‹ด๋‹นํ•˜๋Š” ํŒŒ์ผ.
  • tests.py: Django test์™€ ๊ด€๋ จ๋œ ์ฝ”๋“œ๋“ค์„ ์ž‘์„ฑํ•˜๋Š” ํŒŒ์ผ
  • views.py: View๋“ค์„ ์ž‘์„ฑํ•˜๋Š” ํŒŒ์ผ. ๋ฐ์ดํ„ฐ๋ฅผ ์ ์ ˆํžˆ ๊ฐ€๊ณตํ•˜๋Š” ์—ญํ• .

๐Ÿคซ์ด๊ฑด ๊ธฐ์–ตํ•ด์•ผํ•ด

  • ์–ด๋–ค ํ˜•ํƒœ๋กœ ๋ฐ์ดํ„ฐ ์ €์žฅ ํ˜•ํƒœ๋ฅผ ์„ค์ •ํ• ์ง€? โ†’ Model
  • ์œ ์ €์—๊ฒŒ ๋ณด์—ฌ์ง€๋Š” ํ™”๋ฉด์„ ์ˆ˜์ •ํ•˜๊ณ  ์‹ถ์–ด! โ†’ Template
  • ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•ด์„œ ๊ฐ€๊ณตํ•˜๊ณ  ์‹ถ์–ด! โ†’ View
  • ๊ฐ€๊ณตํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์œ ์ €๊ฐ€ ๋ณด๋Š” ํ™”๋ฉด์— ๋„˜๊ฒจ์ฃผ๊ณ  ์‹ถ์–ด! โ†’ URLConf

๐Ÿ’พ ๋ณ„์ฒจ. URL mapping , path() ์ธ์ˆ˜

URL mapping ์ดํ•ดํ•˜๊ธฐ

์ƒ์„ฑํ•œ polls ์•ฑ์—์„œ urls.py๋ผ๋Š” ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด์•ผ polls์•ฑ์— ๊ด€๋ จ๋œ URL๋งŒ mappingํ•  ์ˆ˜ ์žˆ๊ณ , ๋‹ค๋ฅธ ์ถฉ๋Œ ๋˜ํ•œ ์—†์•จ ์ˆ˜ ์žˆ๋‹ค. mysite.urls.py์—์„œ ์กฐ๊ธˆ ๋” ํฐ ๋ฒ”์ฃผ์˜ url์„ mappingํ•˜๊ณ , ์—ฌ๊ธฐ์„œ polls.urls๊ฐ€ mapping๋˜์–ด polls.py์— mapping๋˜์–ด ๊ตฌํ˜„ํ•œ url๋“ค์„ ์ฝ์„ ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค. ์ด ๋•Œ, include()๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ๋‹ค๋ฅธ URL ํŒจํ„ด์„ ํฌํ•จํ•  ๋•Œ๋งˆ๋‹ค ํ•ญ์ƒ ์‚ฌ์šฉํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

์—ฌ๊ธฐ์„œ path() ํ•จ์ˆ˜์—๋Š” 2๊ฐœ์˜ ํ•„์ˆ˜ ์ธ์ˆ˜์ธ route์™€ view, ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” 2๊ฐ€์ง€์˜ ์ธ์ˆ˜์ธ kwargs์™€ name๊นŒ์ง€ ์ „๋‹ฌ ๊ฐ€๋Šฅํ•˜๋‹ค.

path()์ธ์ˆ˜

  • route: URL ํŒจํ„ด์„ ๊ฐ€์ง„ ๋ฌธ์ž์—ด. ์ผ์น˜ํ•˜๋Š” ํŒจํ„ด์„ ์ฐพ์„ ๋•Œ ๊นŒ์ง€ ์š”์ฒญ๋œ URL์„ ๊ฐ ํŒจํ„ด๊ณผ ๋ฆฌ์ŠคํŠธ์˜ ์ˆœ์„œ๋Œ€๋กœ ๋น„๊ตํ•œ๋‹ค.
  • view: ์ผ์น˜ํ•˜๋Š” ํŒจํ„ด์„ ์ฐพ์œผ๋ฉด, HttpRequest ๊ฐ์ฒด๋ฅผ ์ฒซ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ํ•˜๊ณ , ๊ฒฝ๋กœ๋กœ๋ถ€ํ„ฐ ์ผ์น˜ ๋œ ๊ฐ’์„ ํ‚ค์›Œ๋“œ ์ธ์ˆ˜๋กœ ํ•˜์—ฌ ํŠน์ •ํ•œ view ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
  • kwargs: ์ž„์˜์˜ ํ‚ค์›Œ๋“œ ์ธ์ˆ˜๋“ค์€ dictionary ํ˜•ํƒœ๋กœ view์— ์ „๋‹ฌ๋œ๋‹ค.
  • name: URL์— ์ด๋ฆ„์„ ์ง€์œผ๋ฉด, ํ…œํ”Œ๋ฆฟ์„ ํฌํ•จํ•œ Django ์–ด๋””์—์„œ๋‚˜ ๋ช…ํ™•ํ•˜๊ฒŒ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋‹จ ํ•˜๋‚˜์˜ ํŒŒ์ผ๋งŒ ์ˆ˜์ •ํ•ด๋„ project ๋‚ด์˜ ๋ชจ๋“  URL ํŒจํ„ด์„ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค€๋‹ค.

์ด์ œ ์šฐ๋ฆฌ๋Š” Django๋กœ ํ•˜๋‚˜์˜ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ–ˆ๊ณ , App์„ ๋งŒ๋“ค์—ˆ์œผ๋ฉฐ, ์ƒ์„ฑ ๋œ ํŒŒ์ผ์ด ๋ฌด์Šจ ์—ญํ• ์„ ํ•˜๋Š” ํŒŒ์ผ์ธ์ง€ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค.

์ด์ œ ์ฆ๊ฑฐ์šด ์ฝ”๋”ฉ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค! ๐Ÿคฏ

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