詳解django2中關(guān)于時(shí)間處理策略
一、django中數(shù)據(jù)模型關(guān)于時(shí)間字段的認(rèn)識(shí)
1、 DateField
:可以記錄年月日,映射到數(shù)據(jù)庫是 date
類型
2、 DateTimeField
:可以記錄年月日時(shí)分秒,映射到數(shù)據(jù)庫是 datetime
類型
3、 TimeField
:可以記錄時(shí)分秒,映射到數(shù)據(jù)庫是 time
類型
二、關(guān)于 navie 時(shí)間和 aware 時(shí)間的認(rèn)識(shí)
navie時(shí)間和aware時(shí)間
- navie沒有指定時(shí)區(qū)的,不知道自己的時(shí)間。
- aware指定了時(shí)區(qū),知道自己的時(shí)間。
pytz庫:專門用來處理時(shí)區(qū)的庫,經(jīng)常更新一些時(shí)區(qū)的數(shù)據(jù)
astimezone方法
將一個(gè)時(shí)區(qū)的時(shí)間轉(zhuǎn)換為另一個(gè)時(shí)區(qū)的時(shí)間,這個(gè)方法只能被'aware'類型的時(shí)間調(diào)用,
不能被'navie'類型的時(shí)間調(diào)用
import pytz from datetime import datetime now = datetime.now() #這是一個(gè)navie類型的時(shí)間 utc_timezone = pytz.timezone('UTC') #定義UTC的時(shí)區(qū)對(duì)象 utc_now = now.astimezone(utc_timezone) #將當(dāng)前時(shí)區(qū)時(shí)間轉(zhuǎn)換為UTC時(shí)區(qū)的時(shí)間 >> ValueError: astimezone() cannot be applied to a navie datetime # 會(huì)拋出一個(gè)異常,原因就是因?yàn)閚avie類型的時(shí)間不能調(diào)用astimezone方法 now = now.replace(tzinfo=pytz.timezone('Asia/Shanghai')) utc_now = now.astimezone(utc_timezone) #這時(shí)候就可以進(jìn)行時(shí)區(qū)的轉(zhuǎn)換 #更改時(shí)間
三、在 django 中正確的使用時(shí)間
1、在 settings.py
中配置
TIME_ZONE = 'Asia/Shanghai' # 時(shí)區(qū)的選擇 # 如果USE_TZ=False,那么django獲取到的當(dāng)前時(shí)間就是一個(gè)navie類型的時(shí)間, # 網(wǎng)上很多資料寫的是設(shè)置False,但是實(shí)際開發(fā)過程中設(shè)置True USE_TZ = True
2、在一個(gè) app
的數(shù)據(jù)模型中創(chuàng)建時(shí)間的字段
from django.db import models class ArticleModel(models.Model): """ 文章的模型 """ title = models.CharField(max_length=100, verbose_name='文章標(biāo)題') create_time = models.DateTimeField(verbose_name='文章創(chuàng)建時(shí)間') class Meta(object): db_table = 'article' def __str__(self): return '<ArticleModel>({}, {})'.format(self.title, self.create_time)
3、在視圖類中手動(dòng)的添加一條數(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ù)
實(shí)際上我這是差不多下午13點(diǎn)了,剛好相差8小時(shí)
5、查詢出來的數(shù)據(jù)使用 localtime
函數(shù)轉(zhuǎn)換為本地時(shí)間
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
)中使用(自己會(huì)轉(zhuǎn)換為你電腦本地時(shí)區(qū)的時(shí)間)
<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.py
中 TIME_ZONE = 'Asia/Shanghai'
配置不同的時(shí)區(qū),在用戶頁面展示的結(jié)果也會(huì)不一樣的。
四、在django中數(shù)據(jù)模型使用時(shí)間字段
1、使用字段
create_time = models.DateTimeField(auto_now_add=True, null=True, verbose_name='創(chuàng)建時(shí)間') update_time = models.DateTimeField(auto_now=True, null=True, verbose_name='修改時(shí)間')
2、關(guān)于auto_now_add的認(rèn)識(shí)
auto_now_add會(huì)在第一次添加數(shù)據(jù)的時(shí)候自動(dòng)獲取當(dāng)前時(shí)間
3、關(guān)于auto_now的認(rèn)識(shí)
auto_now會(huì)在每次對(duì)象調(diào)用save方法的時(shí)候更新為當(dāng)前時(shí)間
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
利用Python實(shí)現(xiàn)眨眼計(jì)數(shù)器的示例代碼
這篇文章主要介紹了如何使用Python語言實(shí)現(xiàn)對(duì)視頻中的人物的眨眼進(jìn)行計(jì)數(shù)并描繪在圖表中,文中的實(shí)現(xiàn)步驟講解詳細(xì),感興趣的可以動(dòng)手試一試2022-02-02python如何控制進(jìn)程或者線程的個(gè)數(shù)
這篇文章主要介紹了python如何控制進(jìn)程或者線程的個(gè)數(shù),幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-10-10django實(shí)現(xiàn)類似觸發(fā)器的功能
今天小編就為大家分享一篇django實(shí)現(xiàn)類似觸發(fā)器的功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11python連接FTP服務(wù)器的實(shí)現(xiàn)方法
本文主要介紹了python連接FTP服務(wù)器的實(shí)現(xiàn)方法,主要使用ftp操作進(jìn)行連接FTP服務(wù)器、獲取當(dāng)前目錄文件清單、上傳文件等操作,具有一定的參考價(jià)值,感興趣的可以了解一下2022-06-06如何徹底解決python?NameError:name?'__file__'?is?not?
這篇文章主要給大家介紹了關(guān)于如何徹底解決python?NameError:name?'__file__'?is?not?defined的相關(guān)資料,文中通過圖文將解決的辦法介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02