UnknownTimeZoneError when using RequestContext

2019-06-04 06:27发布

问题:

When using Django 1.4, I am getting an UnknownTimeZoneError for America/Chicago when using RequestContext, but it works without it...any ideas?

Error Message

UnknownTimeZoneError at /my_proj/bad_view/
'America/Chicago'

Source

# This works
def good_view(request):
    data = {}
    return render_to_response('mytemplate.html',
                              data)

# This doesn't
def bad_view(request):
    data = {}
    return render_to_response('mytemplate.html',
                              data,
                              context_instance=RequestContext(request))

回答1:

If you don't care about timezone support you should add USE_TZ = False to your settings.py.

If you want timezone support you should install pytz:

pip install pytz

I believe the error is caused by django getting America/Chicago in the template from the request, but django's limited timezone data doesn't have this timezone. The error will go away if pytz is installed.

You should read django's timezone docs to learn how django 1.4 handles timezones.