Django TodayArchiveView显示第二天的日期(Django TodayArchiveView showing the date for next day)

系统教程 行业动态 更新时间:2024-06-14 17:02:18
Django TodayArchiveView显示第二天的日期(Django TodayArchiveView showing the date for next day)

我只是按照django网站上的说明https://docs.djangoproject.com/en/dev/ref/class-based-views/generic-date-based/ DayArchiveView工作,TodayArchiveView显示第二天的日期。 我的时间有3个小时的差异所以当我只能在晚上9点看到差异时很难调试它。

我有USE_TZ = True,并安装了pytz。 我可以{%load tz%} {{value | localtime}}

I just followed instructions on django website https://docs.djangoproject.com/en/dev/ref/class-based-views/generic-date-based/ DayArchiveView was working and TodayArchiveView was showing the date for the next day. I have a 3 hour difference on my time so it's hard to debug it when I can only see the difference at 9 pm at night.

I have USE_TZ = True, and installed pytz. Can I just do {% load tz %} {{ value|localtime }}

最满意答案

从您的评论中,它似乎正在改变您的settings.TIME_ZONE .TIME_ZONE解决了这个问题。 您还想知道如何确保您网站的访问者获取当天的档案。 这在Django docs中解释为默认时区和当前时区之间的区别,以及下一节中的区别。

简而言之, TodayArchiveView将显示所有用户属于同一日期的条目。 此日期是基于您的settings.TIME_ZONE的当前日期。 这是您期望在大多数网站上看到的内容。 例如,你在你的时区有一篇2月17日的文章,但访客来自一个已经是2月18日的时区。他们将无法看到TodayArchiveView中的文章,即使它是在24小时前发布的! 大多数论坛或博客都在他们的服务器时间和日期工作。

正如文档所解释的那样,可以在视图和模板中转换为最终用户的时区,但如果在模型层完成,则会令人困惑。

From your comments, it seems changing your settings.TIME_ZONE solved the problem. You further wanted to know how to ensure that visitors to your website get the Archives for their current day. This is explained in Django docs as the difference between default timezone and current timezone and in the next section, as wel.

In short, TodayArchiveView will show entries belonging to the same date for all users. This date is the current date based on your settings.TIME_ZONE. This is what you would expect to see in most websites. For e.g. you have an article for Feb 17 in your timezone but a visitor comes from a timezone where it is already Feb 18. They will not be able to see the article in TodayArchiveView even though it was published less than 24 hours ago! Most forums or blogs work in their server time and date.

As the docs explain, there could be conversion to the end user's timezone in views and templates, but it would be confusing if it is done at the model layer.

更多推荐

本文发布于:2023-04-21 18:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/7321654842793bb5e008d65362fcaa73.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:第二天   日期   TodayArchiveView   Django   date

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!