2017-10-18 2 views
0

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> 

답변

0
여기 enter image description here

내 코드입니다 : 이상한 것은 내가 요소를 검사 할 때이 볼 것입니다

변수가 structure이라면 s

01을 제거하십시오.
<li><a href="/structures/{{ structure.id }}/">{{ structure.name }}</a></li> 

SA password protected entry checker 나는이 thread을 발견했습니다. 그러나 장고는 아니지만 바이러스 백신 또는 일부 브라우저 확장 기능입니다.

+0

예를 작성해야! 정말 고맙습니다. –

0

난 당신이처럼 함수를 작성해야한다고 생각 : 다음

def index(request): 
    all_structures = Structure.objects.all() 
    context = { 
     'all_structures': all_structures, 
    } 
    return render(request, 'Structures/index.html', context) 

:

<!DOCTYPE html> 
<html> 
    <head> 
    <meta charset="utf-8"> 
    <title></title> 
    </head> 
    <body> 
    <ul> 
     {% for structure in all_structures %} 
     <li><a href="/structures/{{ structure.id }}/">{{ structure.name }}</a></li> 
     {% endfor %} 
    </ul> 
    </body> 
</html> 

당신은 참으로 {structure.field}하지 {structures.field}

+0

문제는 사실 S 구조에 추가되었습니다. 렌더링 기능이 작동하지 않습니다. 나는 theewboston 채널의 튜토리얼을 보면서 그대로두고 갔다. 정말 고맙습니다. –

+1

이것을 가져 왔습니까?'django.shortcuts import render' ^^ – Deadpool

+0

하하하, 안 그랬어. 그것이 렌더링이 정의되지 않은 이유입니다. 죄송합니다 점심 시간이고, 나는 전혀 집중하지 않습니다. –

관련 문제