2

問題は、テンプレートの価値を得ることができないことです。

Views.py:

from django.shortcuts import get_object_or_404, render_to_response
from django.httpimport HttpResponse


def index(request):
 c='hi'
 return render_to_response('list.html', c)

list.html:

{% extends "base.html" %}

{% block content %}
 list{{ c }}
{% endblock %}

レンダリングされますが、これを引き起こす可能性のあるものはありlistません{{ c }}か?そして、それはエラーを与えません。

4

1 に答える 1

1

render_to_response文字列を直接渡すのに対し 、コンテキストは辞書であると想定しています。

def index(request):
    context = { 'c': 'hi' }
    return render_to_response('list.html', context)

以下のコメントに基づいて、「c」をリストにしたい場合は、代わりに次のようになります。

def index(request):
    context = { 'c': ['hello', 'world'] }
    return render_to_response('list.html', context)

基本的な考え方は、テンプレートで参照する変数のマッピングを作成することです。テンプレートで参照する名前は、辞書の キーです。

于 2013-01-25T16:14:17.383 に答える