通过Django
的API
来存:
python
from django.db import models class Chirp(models.Model): content = models.TextField() pub_time = models.DateTimeField(auto_now=True) def __str__(self): return self.content[:20]+ '...'
template
中:
html
{% for chirp in chirp_page %}
{{ chirp.content }}
{% endfor %}
显示效果示例:
因为我在Django项目里面也设置了时区,所以后面那个时间能正确显示(是这个原因吧。。。),但是通过属性来取的时候就跟数据库里一样了,看下面的图。
数据库中(注意time_zone
):
系统的时区:
Because you only have two choices:
The first item will cause a lot of unnecessary storage and trouble (cannot be directly compared and calculated), so everyone uses the second item. And what time zone to use? By definition, UTC is the most reliable. Other time zones have names, applicable areas, and offsets from UTC that change for various reasons.
The data stored in the database is UTC time. If the time zone is set, it will be converted according to the corresponding rules when displayed
Because this is the best thing to do.
The benefits of doing this are:
"Time zone" is completely a display concept, a "UI" concept;
The storage of time should not have anything to do with "time zone";
It is recommended to store the milliseconds directly
Consider this problem. Your users come from all over the world. If your time is stored in a certain time zone, and users in different time zones visit your page, how do you convert it to the user’s local time? to show?
So what is stored on the server is UTC time. When the user accesses the page, the browser converts UTC time to local time. There is an extension like moment in Flask that does this job specifically. I haven't looked at your code carefully, but I believe that the principles are the same for both python web frameworks.