问题描述:

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 }}

网友答案:

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.

相关阅读:
Top