[TIL] render

H21·2021년 5월 6일
0

Djagngo

목록 보기
3/5

render

render(request, template_name, context=none, content_type=none, status=none, using=none)

render는 위와 같은 parameter를 가진다
이 중 requesttemplate_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 Post

def index(request):
     posts = Post.objects.all().order_by('-created_at')[:3]
     context = {'posts' : posts}
     return render(request, 'posts/index.html', context)

*rendering: 서버로부터 코드로만 되어있는 HTML파일을 받아 브라우저를 통해 볼 수 있는 그래픽의 형태로 출력하는 과정

profile
beyond limit

0개의 댓글