Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法将上下文和变量返回到django中的html_Python_Html_Django - Fatal编程技术网

Python 无法将上下文和变量返回到django中的html

Python 无法将上下文和变量返回到django中的html,python,html,django,Python,Html,Django,我想将上下文和当前时间呈现到我的html文件(从另一个html继承),但它不起作用。有人能帮我解决这个问题吗?非常感谢 这是我的python文件 def homepage(request): now=datetime.datetime.now() list_list = List.objects.order_by('author') context_dict = {'Lists': list_list} return render(request, ('index

我想将上下文和当前时间呈现到我的html文件(从另一个html继承),但它不起作用。有人能帮我解决这个问题吗?非常感谢

这是我的python文件

def homepage(request):
    now=datetime.datetime.now()
    list_list = List.objects.order_by('author')
    context_dict = {'Lists': list_list}
    return render(request, ('index.html',context_dict), {'current_date':now})
这是我的base.html

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>This is my base page.</title>
</head>
<body>
<p>Hello.</p>
{% block content %}{% endblock %}
</body>
{% block footer%}
<p>It is now {{current_date}}.</p>
{% endblock %}
</html>

在渲染中,上下文是设置为
{'current_date':now}
因此,如果您希望在您的上下文中同时使用这两个值,则需要将它们添加到同一个目录中。请执行以下操作:

context_dict = {'Lists': list_list, 'current_date':now}
return render(request, 'index.html', context_dict)

注意,您实际上不需要将日期发送到模板-您可以使用
{%now%}
模板标记直接插入日期。
url(r'^home',homepage),
context_dict = {'Lists': list_list, 'current_date':now}
return render(request, 'index.html', context_dict)