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

< django - 13 >

by A Lim Han 2022. 8. 5.

13. ํผ ๋ ˆ์ด๋ธ” + ์ˆ˜๋™ ํผ ์ž‘์„ฑ + ๋‹ต๋ณ€ ๋“ฑ๋ก

 

 

 

# ํผ ๋ ˆ์ด๋ธ”

1. ์งˆ๋ฌธ ๋“ฑ๋ก ํ™”๋ฉด์˜ 'Subject', 'Content'๋ฅผ ์˜๋ฌธ์ด ์•„๋‹Œ ํ•œ๊ธ€๋กœ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด projects\mysite\pybo\forms.py ํŒŒ์ผ์— labels ์†์„ฑ์„ ์ง€์ •ํ•œ๋‹ค.

 

 

 

2. http://localhost:8000/pybo/question/create ํŽ˜์ด์ง€๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

ํ•œ๊ธ€๋กœ ์„ฑ๊ณต์ ์œผ๋กœ ๋ฐ”๋€œ

 

 

 

 

# ์ˆ˜๋™ ํผ ์ž‘์„ฑ

1. ์ˆ˜๋™ ํผ์„ ์ž‘์„ฑํ•˜๋Š”๋ฐ ํ•„์š”ํ•˜์ง€ ์•Š์€ widget ์†์„ฑ์„ projects\mysite\pybo\forms.py ํŒŒ์ผ์—์„œ ์ œ๊ฑฐํ•œ๋‹ค.

ํŒŒ๋ž€ ๋ถ€๋ถ„ ์ œ๊ฑฐ

 

 

 

2. projects\mysite\templates\pybo\question_form.html ํŒŒ์ผ์˜ ์งˆ๋ฌธ ๋“ฑ๋ก ํ…œํ”Œ๋ฆฟ์„ ์•„๋ž˜์ฒ˜๋Ÿผ ์ˆ˜์ •

Line 7 ~ 30 ์ˆ˜์ •

+ ์ œ๋ชฉ, ๋‚ด์šฉ์— ํ•ด๋‹น๋˜๋Š” HTML์ฝ”๋“œ๋ฅผ ์ง์ ‘ ์ž‘์„ฑํ•จ ( Line 22, 27 )

 

+ question_create ํ•จ์ˆ˜์—์„œ form.is_valid() ๊ฐ€ ์‹คํŒจํ•  ๊ฒฝ์šฐ ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜ ๋‚ด์šฉ์„ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด ์˜ค๋ฅ˜๋ฅผ ํ‘œ์‹œํ•˜๋Š” ์˜์—ญ์„ ์ถ”๊ฐ€ (Line 7 ~ 19)

 

+ ์ œ๋ชฉ(subject) ํ•ญ๋ชฉ์˜ value์— {{ form.subject.value|default_if_none:'' }} ์ฒ˜๋Ÿผ ๊ฐ’ ๋Œ€์ž…  -->  ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๊ฒฝ์šฐ ๊ธฐ์กด์— ์ž…๋ ฅํ–ˆ๋˜ ๊ฐ’์„ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•จ ( Line 24 )

 

+ |default_if_none:''  -->  ํผ ๋ฐ์ดํ„ฐ(form.subject.value)์— ๊ฐ’์ด ์—†์„ ๊ฒฝ์šฐ None ์ด๋ผ๋Š” ๋ฌธ์ž์—ด์ด ํ‘œ์‹œ๋˜๋Š”๋ฐ, ์ด๋•Œ None ์ด ์•„๋‹Œ ๊ณต๋ฐฑ์œผ๋กœ ํ‘œ์‹œํ•˜๋ผ๋Š” ์˜๋ฏธ์˜ ํ…œํ”Œ๋ฆฟ ํ•„ํ„ฐ

+ ์žฅ๊ณ ์˜ ํ…œํ”Œ๋ฆฟ ํ•„ํ„ฐ๋Š” |default_if_none:'' ์ฒ˜๋Ÿผ | ๊ธฐํ˜ธ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ๋จ

 

 

 

3. http://localhost:8000/pybo/question/create/ ํŽ˜์ด์ง€ ํ˜ธ์ถœ --> ์ œ๋ชฉ์—๋งŒ ๋‚ด์šฉ์„ ์ž…๋ ฅํ•˜๊ณ  ๋‚ด์šฉ ๋ถ€๋ถ„์€ ๋น„์›Œ๋‘”๋‹ค. --> ์ €์žฅํ•˜๊ธฐ ํด๋ฆญ

๋‚ด์šฉ ์นธ์— ์•„๋ฌด๊ฒƒ๋„ ์ ์ง€ ์•Š์•„ ๋‚ด์šฉ์„ ์ž…๋ ฅํ•˜๋ผ๋Š” ์˜ค๋ฅ˜๋ฉ”์‹œ์ง€๊ฐ€ ์ถœ๋ ฅ๋จ + ์ œ๋ชฉ์— ์ผ๋˜ ๊ธ€์€ ๊ทธ๋Œ€๋กœ ์œ ์ง€

 

 

 

 

# ๋‹ต๋ณ€ ๋“ฑ๋ก

1. projects\mysite\pybo\forms.py ํŒŒ์ผ์— AnswerForm ์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑ

 

 

 

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

+ question_create์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ AnswerForm์„ ์ด์šฉํ•˜๋„๋ก ๋ณ€๊ฒฝ

+ ๋‹ต๋ณ€ ๋“ฑ๋ก์€ POST ๋ฐฉ์‹๋งŒ ์‚ฌ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์— GET ๋ฐฉ์‹์œผ๋กœ ์š”์ฒญํ•  ๊ฒฝ์šฐ์—๋Š” HttpResponseNotAllowed ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋„๋ก ์ž‘์„ฑ (Line 27, 28)

 

 

 

3. projects\mysite\templates\pybo\question_detail.html ํŒŒ์ผ์˜ ์งˆ๋ฌธ ์ƒ์„ธ ํ…œํ”Œ๋ฆฟ์— ์˜ค๋ฅ˜ ํ‘œ์‹œ๋ฅผ ์œ„ํ•œ ์˜์—ญ ์ถ”๊ฐ€

 

 

 

4. http://localhost:8000/pybo/4/ ํŽ˜์ด์ง€ ํ˜ธ์ถœ  --> ๋‹ต๋ณ€ ๋‚ด์šฉ์„ ์“ฐ์ง€ ์•Š๊ณ  ๋‹ต๋ณ€๋“ฑ๋ก ๋ฒ„ํŠผ ํด๋ฆญ --> ์˜ค๋ฅ˜ ๋ฉ”์„ธ์ง€๊ฐ€ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธ

 

 

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

< django - 15 >  (0) 2022.08.06
< django - 14 >  (0) 2022.08.05
< django - 12 >  (0) 2022.08.05
< django - 11 >  (0) 2022.08.04
< django - 10 >  (0) 2022.08.04