我只是按照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.
更多推荐
发布评论