render
render(request, template_name, context=none, content_type=none, status=none, using=none)
render는 위와 같은 parameter를 가진다
이 중 request
와 template_name
은 필수
화면에 html 파일이 보여진다고 생각
context
를 통해 view에서 사용하던 variable를 html template으로 넘김
context
는 dictionary로
key 값은 template에서 사용할 variable, value 값은 python variable(data)
#veiw.py
from django.shortcuts import render
from .models import Postdef index(request):
posts = Post.objects.all().order_by('-created_at')[:3]
context = {'posts' : posts}
return render(request, 'posts/index.html', context)
*rendering: 서버로부터 코드로만 되어있는 HTML파일을 받아 브라우저를 통해 볼 수 있는 그래픽의 형태로 출력하는 과정