【发布时间】:2021-02-14 06:35:43
【问题描述】:
我有一个使用 django-plotly-dash 包在 Django 中运行的 Plotly Dash 应用程序。 (https://django-plotly-dash.readthedocs.io/en/latest/)
该应用程序已上线:https://www.sunfire.xyz/data/tca
仪表板上有一个日期选择器,元素的代码是这样的:
from datetime import date, datetime, timedelta
html.Div([
dcc.DatePickerRange(
id='date-picker-range',
min_date_allowed=date(2020, 10, 21),
max_date_allowed=date.today(),
initial_visible_month=date.today(),
start_date=date.today() - timedelta(days=7),
end_date=date.today(),
display_format='DD/MM/YYYY',
),
], className="plotly-datepicker"),
max_date_allowed 和默认的end_date 都应该是datetime.date.today()(这是11 月1 日,因为我正在输入这个),但似乎today() 正在返回django 应用程序启动的那一天。
事实上,现在将 max_date_allowed 设置为 10 月 31 日很奇怪,但 end_date 是 10 月 30 日。
如果我重新启动服务器,它会正常工作,并将显示 11 月 1 日。
当应用程序运行多天时,如何让date.today() 正确显示今天的日期?
仅供参考,此应用的 views.py 文件正在导入 Dash 应用 from .dashapps import tca_log_plotter
【问题讨论】:
-
不确定这是否真的是这里的问题;但由于
date.today()返回本地日期,代码将根据运行代码的机器为您提供日期。 -
服务器运行在UTC,settings.py django里面也设置了UTC,和我的当地时间是一样的。所以应该是UTC。
标签: python django datetime plotly-dash python-datetime