2017-03-03 120 views
1

我無法獲得UTC時間轉換爲EST時間。我已經在我的服務器shell中成功完成了它,但是當我在我的視圖中完成同樣的事情時,它會繼續打印出UTC時間,不知道我錯過了什麼。Django UTC時間不轉換

views.py 

from django.utils import timezone 
import pytz 

def today(): 
    today_utc = timezone.now() 
    est = pytz.timezone('US/Eastern') 
    return today_utc.astimezone(est) 

def projections(request): 
    todays_date = today() 
    context = {'todays_date':todays_date} 
    return render(request, 'index.html', context) 

並在我的模板中,我只是做了一個簡單的{{todays_date}},但它繼續打印出UTC時間。

更新: 添加時區模板標籤修復了這個問題,但我認爲我的功能會照顧它。仍然有興趣知道我做錯了什麼....

{% load tz %} 
{% timezone "US/Eastern" %} 
    {{todays_date}} 
{% endtimezone %} 

回答

0

您需要設置時區從settings.py

USE_TZ = False 

TIME_ZONE = 'US/Eastern' 
+0

這將是一種獲得美國/東部時代的方式,但不是處理多個時區的好方法。 – Tom

-1

嘗試使用這樣的:

{{ todays_date|timezone:"America/New_York" }} 

參考documentation

+0

我們在哪裏添加該代碼? –