Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.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模板上下文未显示_Python_Django_Django Templates_Django Views - Fatal编程技术网

Python Django模板上下文未显示

Python Django模板上下文未显示,python,django,django-templates,django-views,Python,Django,Django Templates,Django Views,大家好,我正在使用模板上下文变量,但无论我做什么,当前_datetime视图函数的上下文都不会显示在home.html中,并且没有错误消息。。。我做错了什么 #######view.py def current_datetime(request): now = datetime.datetime.now() context = { "current_date": now } return render(request, "current_date

大家好,我正在使用模板上下文变量,但无论我做什么,当前_datetime视图函数的上下文都不会显示在home.html中,并且没有错误消息。。。我做错了什么

#######view.py

def current_datetime(request):
    now = datetime.datetime.now()
    context = {
        "current_date": now
    }
    return render(request, "current_datetime.html",context)

def home(request):
    title = "My Database"
    context = {
        "template_title": title,
    }
    return render(request,"home.html",context)

#######current_datetime.html

Today is {{ current_date }}

#######home.html

{% extends "base.html" %}
....

{% include 'current_datetime.html' %}

....

它不起作用,因为
currenct\u date
变量不在
home
视图的上下文中

干燥

不要重复你自己。在模板中显示日期的方式是,必须将该变量传递到每个视图的上下文,这完全违背了DRY原则

使用
{%now%}
标记

不要将当前日期作为上下文变量发送,而是使用名为
{%now%}
的模板标记,该标记在模板中显示日期和时间。下面是一个例子:

当前日期.html

将输出:

Today is is 3rd September 2015 08:28

以下是有关

您正在访问的url的详细信息?一个绑定到home或一个绑定到current_datetime?home。。。默认的html..你说的default.html到底是什么意思?问题是“什么是你的url”?url路由到你的视图。将当前_日期添加到上下文的视图不是home,而是另一个。是的,我知道,但我使用了包含标记{%include'current_datetime.html%}。。。这不应该包括在my home.html中吗?如果我访问current_date.html,它会显示日期,这很好。但是如果你看home.html,我有一个包含标签{%include'current_datetime.html%}。。所以当我访问home.html时,它不也应该出现吗?日期时间只是一个例子。。。如果我将其用于其他视图函数,该怎么办?如果在current_date.html模板中使用
{%now%}
标记,它也将显示在
home.html
中。但是,如果您使用上下文变量显示日期,它将不会显示在home.html中,除非
home
视图的上下文中包含
current\u date
变量。谢谢,这很有效。但这并不是我真正想做的。我只是以datetime为例。我有没有办法在一个页面中显示两个视图函数?编辑:nvm,我做了一些搜索,显然这是不可能的。谢谢
Today is is 3rd September 2015 08:28