[ Python ] Django로 웹 서비스 개발하기 ( 9. 간단한 MTV 연동 예제 )

peter_yun
peter_yun
Feb 23, 2017 · 2 min read

장고로 시작하는 2017년

본 글은 코드스쿼드가 진행하는 인프런의 장고 강의를 바탕으로 함
(참고 : http://bit.ly/2jE3ARl )


MTV를 연동해봅시다

(url 설정) url.py를 아래처럼 설정합니다. ▼

(view 설정) .model에서 GuessNumbers 모델을 import하고, 모든 objects 들을 lottos에 할당하여 dictionary 형태로 템플릿에 보냅니다. ▼

(template 설정) 15번 라인에 pre태그를 활용하여 lottos 오브젝트를 삽입하였습니다. ▼

(결과) 아래와 같이 lottos 오브젝트가 삽입되었습니다. ▼

(개선하기) 이제는 for loop를 활용하여 아래와 같이 개선할 수 있습니다. (데이터는 for문이 작동하는지 확인하기 위해 admin에서 직접 추가했습니다.) ▼

이것으로 Model과 View와 Template을 연동시키는 가장 기본적인 방법을 알아보았습니다.


Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade