-
Python Django 9 - 데이터 이동 forward(DB, 데이터)Python Django 2022. 10. 23. 14:27
views.py
from django.shortcuts import render from django.http.response import HttpResponse # Create your views here. def indexFunc(request): ''' msg = '장고 만세' ss = "<html><body>장고 프로젝트 처리 {}</body></html>".format(msg) # return HttpResponse('요청 처리') return HttpResponse(ss) ''' # 클라이언트에게 html 파일을 반환 - 파이썬 값을 html에 담아서 전달 msg = '장고 만세' context = {'msg':msg} # dict type으로 작성해 html 문서에 기술한 장고 template 기호와 매핑 return render(request, 'main.html', context) # forward 방식 기본
전달해야 될 데이터를 dict type으로 작성한 뒤에 rander함수 안에 변수명을 넣어 데이터를 이동시킬 수 있다.
데이터는 갯수 제한이 없다.
main.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>나의 홈피</h1> <p>메세지는 {{ msg }}</p> </body> </html>
html 파일에서 데이터를 사용할 시에는 view처럼 {{ }} 안에 dict key 값을 입력하면 된다.
참고 자료
The Django template language | Django documentation | Django
Django The web framework for perfectionists with deadlines. Toggle theme (current theme: auto) Toggle theme (current theme: light) Toggle theme (current theme: dark) Toggle Light / Dark / Auto color theme Overview Download Documentation News Community Code
docs.djangoproject.com
'Python Django' 카테고리의 다른 글
Python Django 11 - get + post 방식 (0) 2022.10.23 Python Django 10 - static(css, js, image) (0) 2022.10.23 Python Django 8 - controller 사용방법3(Including another URLconf) (0) 2022.10.23 Python Django 7 - controller 사용방법2(class-based views) (0) 2022.10.23 Python Django 6 - controller 사용방법 1(Functhon views) (0) 2022.10.23