Django defaultfilters.date not timezone aware

半腔热情 提交于 2019-12-11 04:28:33

问题


Im trying to create a definition for django admin list_display, but the date filter is not timezone aware. If I just have the updated_date field in list_display, the time is correct.

def get_edited_by(self):
    return u"%s %s - %s" % (self.edited_by.first_name, self.edited_by.last_name,
           defaultfilters.date(self.updated_date, 'SHORT_DATETIME_FORMAT'))

The output is: Bob Bobson - 26.02.2014 18:27 (one hour off)

The output from just the updated_date is: 26. februar 2014 19:27 (correct)

Anyone know how to make defaultfilters.date timezone aware?

Edit:

My settings:

TIME_ZONE = 'Europe/Oslo'
USE_I18N = True
USE_L10N = True
USE_TZ = True

Edit2:

pytz==2013.9 Django==1.6.2

Edit 3:

Tried this

from django.utils.dateformat import DateFormat
def get_edited_by(self):
    return u"%s %s - %s" % (self.edited_by.first_name, self.edited_by.last_name, DateFormat.format(self.updated_date, "SHORT_DATETIME_FORMAT"))

But I get this error: unbound method format() must be called with DateFormat instance as first argument (got datetime instance instead)

Edit 4:

from django.utils import date format
def get_edited_by(self):
    return u"%s %s - %s" % (self.edited_by.first_name, self.edited_by.last_name, dateformat.format(self.updated_date, 'SHORT_DATETIME_FORMAT'))

Gives this output: Bob Bobsen - th23+0100RCET_onsPMCETFebruarCET0FebFebruar_februar+0100RFebPMCET

来源:https://stackoverflow.com/questions/22050530/django-defaultfilters-date-not-timezone-aware

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!