Python 如何在navbar中为django中的所有模板设置datetime?
我的django项目中有3个HTMLPython 如何在navbar中为django中的所有模板设置datetime?,python,django,templates,navbar,Python,Django,Templates,Navbar,我的django项目中有3个HTML - base.html - navbar.html - home.html 我想在导航栏中设置日期时间字段,因为我在家的时候。我可以在那里看到时间。我寻找类似{{request.user}的东西,所以我在Navbar中使用它 我知道我可以定义: time = datetime.datetime.now() 用于定义日期时间 但我无法为导航栏定义任何上下文,因为其中没有def Views.py 我能做什么? tnx用于响应。用于设置全局变量 如何在D
- base.html
- navbar.html
- home.html
我想在导航栏中设置日期时间字段,因为我在家的时候。我可以在那里看到时间。我寻找类似{{request.user}的东西,所以我在Navbar中使用它
我知道我可以定义:
time = datetime.datetime.now()
用于定义日期时间
但我无法为导航栏定义任何上下文,因为其中没有def
Views.py
我能做什么?
tnx用于响应。用于设置全局变量
如何在Django中使用上下文处理器
创建一个返回当前时间的函数
contextprocessor.py
在django中将函数注册为上下文处理器
设置.py
全局使用上下文处理器
base.html
你是什么意思,但我不能为导航栏定义任何上下文,因为Views.py中没有def?这对我帮助很大。
from datetime import datetime
def time_now(request):
return {'time_now': datetime.now()}
TEMPLATE_CONTEXT_PROCESSORS = (
...
'contextprocessor.time_now',
)
<ul>
<li class="nav-item">
<a class="nav-link">{{ time_now | date:'Y-m-d H:i' }}</a>
</li>
</ul>
</nav>