๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โœ’๏ธ Python Programming/Frameworks

< django - 12 >

by A Lim Han 2022. 8. 5.

12. ํผ๊ณผ ์งˆ๋ฌธ ๋“ฑ๋ก + URL ๋งคํ•‘ + ๋ทฐ ํ•จ์ˆ˜ + ํ…œํ”Œ๋ฆฟ + GET๊ณผ POST

 

 

 

# ์งˆ๋ฌธ ๋“ฑ๋ก๊ณผ URL ๋งคํ•‘

1. projects\mysite\templates\pybo\question_list.html ํŒŒ์ผ์— ์งˆ๋ฌธ ๋“ฑ๋กํ•˜๊ธฐ ๋ฒ„ํŠผ์„ ๋งŒ๋“ ๋‹ค.

Line 30์— ์žˆ๋Š” ๋‚ด์šฉ ์ถ”๊ฐ€ํ•จ

+ <a href="..."> ๊ณผ ๊ฐ™์€ ๋งํฌ์ด์ง€๋งŒ ๋ถ€ํŠธ์ŠคํŠธ๋žฉ์˜ btn btn-primary ํด๋ž˜์Šค๋ฅผ ์ ์šฉํ•˜๋ฉด ๋ฒ„ํŠผ์œผ๋กœ ๋ณด์ž„  -->  ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ pybo:question_create ๋ณ„์นญ์— ํ•ด๋‹น๋˜๋Š” URL์ด ํ˜ธ์ถœ 

 

 

 

2. projects\mysite\pybo\urls.py ํŒŒ์ผ์— pybo:question_create ๋ณ„์นญ์— ํ•ด๋‹น๋˜๋Š” URL ๋งคํ•‘ ๊ทœ์น™์„ ์ถ”๊ฐ€ํ•˜์—ฌ views.question_create ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋„๋ก ๋งคํ•‘

Line 11์˜ ๋‚ด์šฉ ์ถ”๊ฐ€

 

 

 

 

# ํผ(Form)๊ณผ ๋ทฐ ํ•จ์ˆ˜

: ํŽ˜์ด์ง€๊ฐ€ ์š”์ฒญ๋ ๋•Œ ์ „๋‹ฌ๋˜๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋“ค์„ ์‰ฝ๊ฒŒ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ํด๋ž˜์Šค๋กœ, ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์˜ ๋ˆ„๋ฝ ์—ฌ๋ถ€ ๋ฐ ํŒŒ๋ผ๋ฏธํ„ฐ ํ˜•์‹์˜ ์ ์ ˆ์„ฑ ์—ฌ๋ถ€ ๋“ฑ์„ ๊ฒ€์ฆํ•˜๋Š”๋ฐ ์“ฐ์ธ๋‹ค. 

 

- HTML ์ž๋™ ์ƒ์„ฑ ๊ธฐ๋Šฅ๊ณผ ํผ์— ์—ฐ๊ฒฐ๋˜์–ด์žˆ๋Š” ๋ชจ๋ธ์„ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๊ธฐ๋Šฅ์ด ์žˆ๋‹ค.

 

1. projects\mysite\pybo\forms.py ํŒŒ์ผ์— QuestionForm์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ƒˆ๋กœ ์ž‘์„ฑํ•œ๋‹ค.

+ QuestionForm์€ ๋ชจ๋ธ ํผ(forms.ModelForm)์„ ์ƒ์†ํ•จ

+ ๋ชจ๋ธ ํผ : ๋ชจ๋ธ(Model)๊ณผ ์—ฐ๊ฒฐ๋œ ํผ์œผ๋กœ ํผ์„ ์ €์žฅํ•˜๋ฉด ์—ฐ๊ฒฐ๋œ ๋ชจ๋ธ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ• ์ˆ˜ ์žˆ๋Š” ํผ์œผ๋กœ, ์ด๋„ˆ ํด๋ž˜์Šค์ธ Meta ํด๋ž˜์Šค( ์‚ฌ์šฉํ•  ๋ชจ๋ธ๊ณผ ๋ชจ๋ธ์˜ ์†์„ฑ์„ ์ ์Œ )๋ฅผ ๊ผญ ํ•„์š”๋กœ ํ•œ๋‹ค.

 

+ ์žฅ๊ณ ์˜ ํผ

์ผ๋ฐ˜ ํผ ๋ชจ๋ธ ํผ
forms.Form forms.ModelForm

 

+ Line 8์—์„œ QuestionForm์—์„œ ์‚ฌ์šฉํ•  Question ๋ชจ๋ธ์˜ ์†์„ฑ์„ ์ •์˜ํ•จ

 

 

 

2. projects\mysite\pybo\views.py ํŒŒ์ผ์— views.question_create ํ•จ์ˆ˜๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑ

Line 4, 21 ~ 23 ์˜ ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•จ

+ question_create ํ•จ์ˆ˜๋Š” ์œ„์—์„œ ์ž‘์„ฑํ•œ QuestionForm์„ ์‚ฌ์šฉ

+ Line 23  -->  render ํ•จ์ˆ˜์— ์ „๋‹ฌํ•œ {'form': form}์€ ํ…œํ”Œ๋ฆฟ์—์„œ ์งˆ๋ฌธ ๋“ฑ๋ก์‹œ ์‚ฌ์šฉํ•  ํผ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์‚ฌ์šฉ๋จ

 

 

 

 

# ํ…œํ”Œ๋ฆฟ + GET๊ณผ POST

1. pybo/question_form.html ํ…œํ”Œ๋ฆฟ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑ

ํŒŒ์ผ๋ช…์€&nbsp;projects\mysite\templates\pybo\question_form.html

+ ํ…œํ”Œ๋ฆฟ์—์„œ ์‚ฌ์šฉํ•œ {{ form.as_p }}์˜ form = question_create ํ•จ์ˆ˜์—์„œ ์ „๋‹ฌํ•œ QuestionForm์˜ ๊ฐ์ฒด

+ {{ form.as_p }}๋Š” ํผ์— ์ •์˜ํ•œ subject, content ์†์„ฑ์— ํ•ด๋‹นํ•˜๋Š” HTML ์ฝ”๋“œ๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑ

 

+ ์—ฌ๊ธฐ์„œ๋Š” form ํƒœ๊ทธ์— action ์†์„ฑ์„ ์ง€์ •ํ•˜์ง€ ์•Š์•˜์Œ -->  form ํƒœ๊ทธ์— action ์†์„ฑ์„ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ํ˜„์žฌ ํŽ˜์ด์ง€์˜ URL์ด ๋””ํดํŠธ action์œผ๋กœ ์„ค์ •๋จ

 

 

 

2. http://localhost:8000/pybo/ ํŽ˜์ด์ง€๋ฅผ ์š”์ฒญํ•œ๋‹ค.

์งˆ๋ฌธ ๋ชฉ๋ก ํ™”๋ฉด ํ•˜๋‹จ์— "์งˆ๋ฌธ ๋“ฑ๋กํ•˜๊ธฐ" ๋ฒ„ํŠผ์ด ์ถ”๊ฐ€๋จ

+ subject์™€ content ์ž…๋ ฅ ์ฐฝ์— ๊ฐ’์„ ์ž…๋ ฅํ•˜๊ณ  ์ €์žฅํ•˜๊ธฐ๋ฅผ ๋ˆŒ๋Ÿฌ๋„ question_create ํ•จ์ˆ˜์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์•„์ง ์ž‘์„ฑํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ฌด๋Ÿฐ ๋ฐ˜์‘์ด X

 

 

 

3.  projects\mysite\pybo\views.py ํŒŒ์ผ์˜ question_create ํ•จ์ˆ˜๋ฅผ ์•„๋ž˜์ฒ˜๋Ÿผ ์ˆ˜์ •

+ ๋™์ผํ•œ URL ์š”์ฒญ์„ POST, GET ์š”์ฒญ ๋ฐฉ์‹์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ์ฒ˜๋ฆฌ

์งˆ๋ฌธ ๋ชฉ๋ก ํ™”๋ฉด์—์„œ "์งˆ๋ฌธ ๋“ฑ๋กํ•˜๊ธฐ" ๋ฒ„ํŠผ์„ ํด๋ฆญํ•œ ๊ฒฝ์šฐ 

-->  /pybo/question/create/ ํŽ˜์ด์ง€๊ฐ€ GET ๋ฐฉ์‹์œผ๋กœ ์š”์ฒญ( ๋งํฌ๋ฅผ ํ†ตํ•ด ํŽ˜์ด์ง€๋ฅผ ์š”์ฒญํ•  ๊ฒฝ์šฐ ๋ฐ˜๋“œ์‹œ GET ๋ฐฉ์‹์ด ์‚ฌ์šฉ๋œ๋‹ค.

-->  question_create ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰  -->  request.method ๊ฐ’์ด GET์ด ๋จ 

-->  if .. else .. ๊ตฌ๋ฌธ์—์„œ else ๊ตฌ๋ฌธ์„ ํƒ€๊ฒŒ ๋˜์–ด ์งˆ๋ฌธ ๋“ฑ๋ก ํ™”๋ฉด ๋ Œ๋”๋ง

 

: ์งˆ๋ฌธ ๋“ฑ๋ก ํ™”๋ฉด์—์„œ subject, content ํ•ญ๋ชฉ์— ๊ฐ’์„ ๊ธฐ์ž…ํ•˜๊ณ  "์ €์žฅํ•˜๊ธฐ" ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ ๊ฒฝ์šฐ

-->  /pybo/question/create/ ํŽ˜์ด์ง€๋ฅผ POST ๋ฐฉ์‹์œผ๋กœ ์š”์ฒญ( form ํƒœ๊ทธ์— action ์†์„ฑ์ด ์ง€์ •๋˜์ง€ ์•Š์œผ๋ฉด ํ˜„์žฌ ํŽ˜์ด์ง€๊ฐ€ ๋””ํดํŠธ action์œผ๋กœ ์„ค์ •๋˜๊ธฐ ๋•Œ๋ฌธ )

-->  question_create ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰  -->  request.method ๊ฐ’์€ POST๊ฐ€ ๋˜์–ด ๋‹ค์Œ ์ฝ”๋“œ ๋ธ”๋ก ์‹คํ–‰

 

 

+ GET ๋ฐฉ์‹ : form = QuestionForm() ์ฒ˜๋Ÿผ QuestionForm์„ ์ธ์ˆ˜ ์—†์ด ์ƒ์„ฑ

VS

POST ๋ฐฉ์‹ : form = QuestionForm(request.POST) ์ฒ˜๋Ÿผ request.POST๋ฅผ ์ธ์ˆ˜๋กœ ์ƒ์„ฑ

--> ์ด ๊ฒฝ์šฐ request.POST์— ๋‹ด๊ธด subject, content ๊ฐ’์ด QuestionForm์˜ subject, content ์†์„ฑ์— ์ž๋™์œผ๋กœ ์ €์žฅ๋˜์–ด ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋จ. ์ด๋•Œ request.POST์—๋Š” ํ™”๋ฉด์—์„œ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๋‚ด์šฉ๋“ค์ด ์žˆ์Œ.

 

 

+ Line 24 => ํผ์ด ์œ ํšจํ•œ ๊ฒฝ์šฐ์—๋Š”

: form.is_valid()๋Š” form์ด ์œ ํšจํ•œ์ง€๋ฅผ ๊ฒ€์‚ฌํ•จ. form ์ด ์œ ํšจํ•œ ๊ฒฝ์šฐ if form.is_valid(): ์ดํ›„์˜ ๋ฌธ์žฅ์ด ์ˆ˜ํ–‰๋˜์–ด ์งˆ๋ฌธ ๋ฐ์ดํ„ฐ๊ฐ€ ์ƒ์„ฑ๋จ

 

+ Line 25 => ์ž„์‹œ๋กœ ์ €์žฅํ•˜์—ฌ question ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ด๋ฐ›๋Š”๋‹ค. ( ์•„์ง ์‹ค์ œ ๋ฐ์ดํ„ฐ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅ๋˜์ง€ ์•Š์Œ. )

: form.save(commit=False)๋Š” form์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋กœ Question ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ๋กœ, commit=False๋Š” ์ž„์‹œ ์ €์žฅ์„ ์˜๋ฏธํ•จ.

 

+ Line 26 => ์‹ค์ œ๋กœ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด ์ž‘์„ฑ ์ผ์‹œ๋ฅผ ์„ค์ •

: QuestionForm์—๋Š” ํ˜„์žฌ subject, content ์†์„ฑ๋งŒ ์ •์˜๋˜์–ด ์žˆ๊ธฐ์— ์ž‘์„ฑ ์ผ์‹œ๊ฐ’์„ ๋”ฐ๋กœ ์„ค์ •ํ•ด ์ค˜์•ผ ํ•จ. ( create_date ์†์„ฑ์€ ๋ฐ์ดํ„ฐ ์ €์žฅ ์‹œ์ ์— ์ƒ์„ฑํ•ด์•ผ ํ•˜๋Š” ๊ฐ’์ด๋ฏ€๋กœ QuestionForm์— ๋“ฑ๋กํ•˜์—ฌ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Œ )

 

+ Line 27 => ๋ฐ์ดํ„ฐ๋ฅผ ์‹ค์ œ๋กœ ์ €์žฅ

 

 

 

4. ๋‹ค์‹œ http://localhost:8000/pybo/ ํŽ˜์ด์ง€๋ฅผ ์š”์ฒญ --> ์งˆ๋ฌธ ๋“ฑ๋กํ•˜๊ธฐ --> ์งˆ๋ฌธ ๋“ฑ๋ก ํ™”๋ฉด์— ๋‚ด์šฉ ๋ฐ ์ œ๋ชฉ ์ž…๋ ฅ --> ์ €์žฅํ•˜๊ธฐ ํด๋ฆญ

http://localhost:8000/pybo/&nbsp;ํŽ˜์ด์ง€๋ฅผ ์š”์ฒญ
์‚ฐ์ˆ˜ ์งˆ๋ฌธ ๋“ฑ๋ก --> ์ €์žฅ
์งˆ๋ฌธ์ด ์„ฑ๊ณต์ ์œผ๋กœ ๋“ฑ๋ก๋จ

 

 

 

'โœ’๏ธ Python Programming > Frameworks' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

< django - 14 >  (0) 2022.08.05
< django - 13 >  (0) 2022.08.05
< django - 11 >  (0) 2022.08.04
< django - 10 >  (0) 2022.08.04
< django - 9 >  (0) 2022.08.03