溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

詳解django2中關(guān)于時間處理策略

發(fā)布時間:2020-09-03 06:40:47 來源:腳本之家 閱讀:175 作者:水痕001 欄目:開發(fā)技術(shù)

一、django中數(shù)據(jù)模型關(guān)于時間字段的認識

1、 DateField :可以記錄年月日,映射到數(shù)據(jù)庫是 date 類型

2、 DateTimeField :可以記錄年月日時分秒,映射到數(shù)據(jù)庫是 datetime 類型

3、 TimeField :可以記錄時分秒,映射到數(shù)據(jù)庫是 time 類型

二、關(guān)于 navie 時間和 aware 時間的認識

navie時間和aware時間

  • navie沒有指定時區(qū)的,不知道自己的時間。
  • aware指定了時區(qū),知道自己的時間。

pytz庫:專門用來處理時區(qū)的庫,經(jīng)常更新一些時區(qū)的數(shù)據(jù)

astimezone方法

將一個時區(qū)的時間轉(zhuǎn)換為另一個時區(qū)的時間,這個方法只能被'aware'類型的時間調(diào)用,

不能被'navie'類型的時間調(diào)用

import pytz
from datetime import datetime
now = datetime.now() #這是一個navie類型的時間
utc_timezone = pytz.timezone('UTC') #定義UTC的時區(qū)對象
utc_now = now.astimezone(utc_timezone) #將當(dāng)前時區(qū)時間轉(zhuǎn)換為UTC時區(qū)的時間
>> ValueError: astimezone() cannot be applied to a navie datetime
# 會拋出一個異常,原因就是因為navie類型的時間不能調(diào)用astimezone方法

now = now.replace(tzinfo=pytz.timezone('Asia/Shanghai'))
utc_now = now.astimezone(utc_timezone)
#這時候就可以進行時區(qū)的轉(zhuǎn)換

#更改時間

三、在 django 中正確的使用時間

1、在 settings.py 中配置

TIME_ZONE = 'Asia/Shanghai' # 時區(qū)的選擇
# 如果USE_TZ=False,那么django獲取到的當(dāng)前時間就是一個navie類型的時間,
# 網(wǎng)上很多資料寫的是設(shè)置False,但是實際開發(fā)過程中設(shè)置True
USE_TZ = True

2、在一個 app 的數(shù)據(jù)模型中創(chuàng)建時間的字段

from django.db import models


class ArticleModel(models.Model):
 """
 文章的模型
 """
 title = models.CharField(max_length=100, verbose_name='文章標題')
 create_time = models.DateTimeField(verbose_name='文章創(chuàng)建時間')

 class Meta(object):
 db_table = 'article'

 def __str__(self):
 return '<ArticleModel>({}, {})'.format(self.title, self.create_time)

3、在視圖類中手動的添加一條數(shù)據(jù)

from django.shortcuts import render
from django.views import View
# 引入模塊
from django.utils.timezone import now, localtime
from . import models


class ArticleView(View):
 """
 文章的視圖類
 """

 def get(self, request, *args, **kwargs):
 models.ArticleModel.objects.create(title='第一篇文章', create_time=now())
 return render(request, 'article.html')

4、查看數(shù)據(jù)庫數(shù)據(jù)

實際上我這是差不多下午13點了,剛好相差8小時

詳解django2中關(guān)于時間處理策略

5、查詢出來的數(shù)據(jù)使用 localtime 函數(shù)轉(zhuǎn)換為本地時間

from django.shortcuts import render
from django.views import View
# 引入模塊
from django.utils.timezone import now, localtime
from . import models


class ArticleView(View):
 """
 文章的視圖類
 """

 def get(self, request, *args, **kwargs):
 result = models.ArticleModel.objects.get(pk=1)
 print(result)
 print(localtime(result.create_time))
 return render(request, 'article.html')

6、在模板( html )中使用(自己會轉(zhuǎn)換為你電腦本地時區(qū)的時間)

<p>{{ article.title }}</p>
<p>{{ article.create_time }}</p>
<!--直接使用django內(nèi)置過濾器格式化數(shù)據(jù)-->
<p>{{ article.create_time | date:"Y-m-d H:i:s" }}</p>

7、如果你在 settings.pyTIME_ZONE = 'Asia/Shanghai' 配置不同的時區(qū),在用戶頁面展示的結(jié)果也會不一樣的。

四、在django中數(shù)據(jù)模型使用時間字段

1、使用字段

create_time = models.DateTimeField(auto_now_add=True, null=True, verbose_name='創(chuàng)建時間')
update_time = models.DateTimeField(auto_now=True, null=True, verbose_name='修改時間')

2、關(guān)于auto_now_add的認識

auto_now_add會在第一次添加數(shù)據(jù)的時候自動獲取當(dāng)前時間

3、關(guān)于auto_now的認識

auto_now會在每次對象調(diào)用save方法的時候更新為當(dāng)前時間

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI