Python 如何在django中将列表从视图传递到模板
我试图在Django中将列表从视图传递到模板 在我的文件wiew.py中,我定义了名为hour的视图Python 如何在django中将列表从视图传递到模板,python,django,Python,Django,我试图在Django中将列表从视图传递到模板 在我的文件wiew.py中,我定义了名为hour的视图 # This Python file uses the following encoding: utf-8 from django.shortcuts import render from django.http import HttpResponse from datetime import datetime from django.shortcuts import render_to_r
# This Python file uses the following encoding: utf-8
from django.shortcuts import render
from django.http import HttpResponse
from datetime import datetime
from django.shortcuts import render_to_response
# Create your views here.
def hour(request):
now = datetime.now()
list = ['Bern','Bob','Eufronio','Epifanio','El pug']
return render_to_response('hour.html',list)
在我看来,我正在使用shorcuts
我有一个名为hour.html的模板,因此:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="CONTENT-TYPE" content="text/html; charset=utf-8">
<title>Date</title>
</head>
<body>
The names are:
{%for item in list%}
<li>{{item}}</li>
{% endfor %}
</body>
</html>
日期
名单如下:
{列表%中的项目的%s}
{{item}}
{%endfor%}
但是我的模板hour.html在浏览器中显示为空。如何将列表从视图发送到模板。感谢大家的关注
列表
是python内置的序列类型,您应该避免为变量名使用泛型名称以避免冲突。您可以阅读更多关于Python内置类型的信息
但是,为此,您只需要在上下文中传递:
return render_to_response('hour.html', {"list": list})
您应该使用
return render\u to\u response('hour.html',{“list”:list})
或者返回渲染(请求'hour.html',{“list”:list})
第二个
渲染
需要来自django的。快捷方式导入渲染
你好,谢谢你的信息,尽管我已将名称更改为myList,但这对我不起作用。render_to_response函数可以发送python对象?是的,它可以传递任何易怒的消息。清单、口述等。完美,它能工作。这意味着我可以传递任何python对象,但总是在字典中?