为什么Django不渲染我的模板?

时间:2015-02-06 02:37:18

标签: python html django

我正在研究Django应用程序,我只是想将数据推送到前端进行显示。

在我的views.py这里是我所拥有的:

def index(request):
    ...
    context = RequestContext(request)

    rooms = dict(db.studybug.find_one())

    timeRange = [room.encode('utf-8') for room in rooms['timeRange']]

    return render_to_response('studybug/index.html', timeRange, context)

此处,timeRange是一个包含以下内容的列表:

timeRange = ['Room 203A 10:00 AM \xc2\xa0', 'Room 203A 10:30 AM \xc2\xa0', 'Room 203A 11:00 AM \xc2\xa0', 'Room 203A 11:30 AM \xc2\xa0', 'Room 203A 12:00 PM \xc2\xa0', 'Room 203A 12:30 PM \xc2\xa0', 'Room 203A 3:00 PM \xc2\xa0', 'Room 203A 3:30 PM \xc2\xa0', 'Room 203A 4:00 PM \xc2\xa0', 'Room 203A 4:30 PM \xc2\xa0', 'Room 203A 5:00 PM \xc2\xa0', 'Room 203A 5:30 PM \xc2\xa0', 'Room 203A 6:00 PM \xc2\xa0', 'Room 203A 6:30 PM \xc2\xa0', 'Room 203A 7:00 PM \xc2\xa0', 'Room 203A 7:30 PM \xc2\xa0', 'Room 203A 8:00 PM \xc2\xa0', 'Room 203A 8:30 PM \xc2\xa0', 'Room 203A 9:00 PM \xc2\xa0', 'Room 203A 9:30 PM \xc2\xa0', 'Room 203A 10:00 PM \xc2\xa0', 'Room 203A 10:30 PM \xc2\xa0', 'Room 203A 11:00 PM \xc2\xa0', 'Room 203A 11:30 PM \xc2\xa0']

然后在我的模板(index.html)中,我有以下循环:

<div class="row">
    ...
  <ul>
    {% for item in timeRange %}
        <li>{{ item }}</li>
    {% endfor %}
  </ul>

</div>

但是,尽管在后端生成了列表,但网页上没有显示任何内容。我知道列表存在,但Django的渲染引擎不会显示它。

我错过了一些明显的东西吗?

谢谢,

1 个答案:

答案 0 :(得分:1)

render_to_response第二个参数,应该是包含您数据的dict,您传递的是list

您的render_to_response应如下所示:

 return render_to_response('studybug/index.html', {'timeRange':timeRange}, context)
相关问题