【发布时间】:2018-09-18 12:07:46
【问题描述】:
我正在尝试将 views.py 中的书籍列表传递给 html 模板。
我采用了日期时间示例并对其进行了修改,但它不起作用。
这是我的views.py:
def theBooks(request):
t = template.loader.get_template('templates/index.html')
the_books = Book.objects.all()
c = template.Context({'books': the_books})
html = t.render(c)
return HttpResponse(html)
我的模板是:
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<title>Current Time</title>
</head>
<body>
{# This is a comment #}
{# check the existence of now variable in the template using if tag #}
{% if now %}
<p>The books are: {{ books }}</p>
{% else %}
<p>now variable is not available</p>
{% endif %}
</body>
</html>
【问题讨论】:
-
{'books': the: books}应该是{'books': the_books} -
我想在stackoverflow.com/questions/12761173/…之前问过类似的问题
标签: django django-templates django-views