如何在Django中将列表从视图传递到模板 [英] How to pass a list from a view to template in django

查看:46
本文介绍了如何在Django中将列表从视图传递到模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将列表从视图传递到Django中的模板.

I am trying pass to list from a view to template in Django.

在我的文件wiew.py中,我定义了名为hour的视图

In my file wiew.py I define the view named 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_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)

我在视图中使用快捷方式.

I'm using shorcuts in my view.

我有一个名为hour.html的模板,因此:

I have a template named hour.html so this way:

<!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>

但是我的模板hour.html在浏览器中显示为空.如何将列表从视图发送到模板.感谢您的游览关注

But my template hour.html is showed empty in the browser. How to can I send the list from my view to template. Thanks for tour attention

推荐答案

您应使用 return render_to_response('hour.html',{"list":list})

返回渲染(请求,"hour.html",{列表":列表})

第二个 render 需要来自django的 .shortcuts导入渲染

The second render need from django.shortcuts import render

这篇关于如何在Django中将列表从视图传递到模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆