Django Setting里默认的时区是UTC(格林威治时间)
TIME_ZONE = 'UTC'
USE_TZ = True
为了方便使用把它修改为北京时间,如下:
TIME_ZONE = 'Asia/Shanghai'
USE_TZ = True
这样修改后出现一个问题,时区的设置只是对渲染html有效,models.DateTimeField仍然会将时间保存为UTC时间。测试验证如下:
1.在Django创建Mysite项目并下挂Blog(配置、代码省略),在Blog.Models中创建Post类。Post代码如下:
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
class Post(models.Model):
STATUS_CHOICES = (('draft','Draft'),('published','Published'))
title = models.CharField(max_length=250)
slug=models.SlugField(max_length=250,unique_for_date='publish')
author = models.ForeignKey(User,on_delete=models.CASCADE,related_name='blog_posts')
body=models.TextField()
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
update = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10,choices = STATUS_CHOICES,default='draft')
objects = models.Manager()
在Post中设置了3个DateTimeField属性的字段,分别是publish,created,update。
2.在views中创建post_list
from django.shortcuts import render
from .models import Post
# Create your views here.
def post_list(request):
posts = Post.published.all()
return render(request,'blog/post/list.html',{'posts':posts})
3. 在blog/urls.py中配置路径
from django.urls import path
from . import views
app_name = 'blog'
urlpatterns = [
#post views
path('',views.post_list,name='post_list'),
]
4.在blog下创建templates文件夹,并创建base.html。接着在templates下创建blog文件夹,并在blog文件夹中创建list.html
base.html
{% load static %}
<!DOCTYPE html>
<html>
<head>
<title>
{% block title %}{% endblock %}
</title>
<link href= "{% static 'blog.css' %}" rel="stylesheet">
</head>
<body>
<div id="content">
{% block content %}
{% endblock %}
</div>
<div id = "sidebar">
<h2>My blog</h2>
<p>This is my blog</p>
</div>
</body>
</html>
list.html
{% extends "blog/base.html" %}
{% block title %}My Blog{% endblock %}
{% block content %}
<h1>My Blog</h1>
{% for post in posts %}
<h2>
<a href = "{{post.get_absolute_url}}">
{{post.title}}
{{post.publish}}
</a>
</h2>
<p class = "date">
Published {{post.publish}} by {{post.author}}
</p>
{{post.body|truncatewords:50|linebreaks}}
{% endfor %}
{% endblock %}
5. 修改Django自带的admin模块(blog/admin.py),使之可以用于后台编写文章。
from django.contrib import admin
from .models import Post
# Register your models here.
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display= ('title','slug','author','publish','status')
list_filter = ('status','created','publish','author')
search_fields = ('title','body')
prepopulated_fields = {'slug':('title',)}
raw_id_fields = ('author',)
date_hierarchy = 'publish'
ordering = ('status','publish')
6.启动本地服务器,在localhost:8000/admin编写测试文章
7.在localhost:8000/blog下查看文章,发现一切正常,时区是东8区(北京时间)
8.到python shell查看后台数据,问题出现了,在Post中created和publish字段保存的时间仍然是UTC时间。
>>> from blog.models import Post
>>> newpost=Post.objects.get(id=10)
>>> print("Created time is " +str(newpost.created))
Created time is 2022-02-19 03:13:43.407694+00:00
>>> print("Publish time is "+str(newpost.publish))
Publish time is 2022-02-19 03:08:53+00:00
>>>
这个问题带来的直接影响是后期用reverse()按日期反转url时,会因为日期不一致(差8小时)导致反转得到的url出错。因为reverse()通过request读到的是post_list渲染在html中posts对应字段的时间,即我们浏览器显示的时间。