Django에서 템플릿을 만들고 매우 간단한 HTML 코드를 초기화하여 views.py 파일에서로드 된 일부 변수를 테스트 해 보았습니다. HTML 파일이로드되었지만 변수가 없습니다.Django 템플릿 뷰에서 변수를로드하지 않습니다.
views.py :
from django.http import HttpResponse
from django.template import loader
from .models import Structure
def index(request):
all_structures = Structure.objects.all()
template = loader.get_template('Structures/index.html')
context = {
'all_structures': all_structures,
}
return HttpResponse(template.render(context, request))
def detail(request, structure_id):
return HttpResponse("<h2>Details for Structure id " + str(structure_id) + "</h2>")
index.html을 :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<ul>
{% for structure in all_structures %}
<li><a href="/structures/{{ structures.id }}/">{{ structures.name }}</a></li>
{% endfor %}
</ul>
</body>
</html>
예를 작성해야! 정말 고맙습니다. –