ZoneInfoNotFoundError: 'No time zone found with key utc'
Asked Answered
D

7

26

While trying to load my webpage on the browser, I got the message.

A server error occurred. Please contact the administrator

And when I go back to check my termimal, I see the message

zoneinfo._common.ZoneInfoNotFoundError: 'No time zone found with key UTC'

I have checked but don't know what's wrong. I even tried changing the UTC to my original timezone, but it still didn't work.

P.S: I started getting this error after I tried working on templates inheritance

Here's what my settings.py file looks like

INSTALLED_APPS = [
    'newapp.apps.NewappConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'new.urls'



TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'new.wsgi.application'


# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/

STATIC_URL = 'static/'

# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
Deus answered 10/2, 2022 at 1:52 Comment(0)
A
62

Add tzdata to your requirements or pip install tzdata

Add answered 10/2, 2022 at 11:19 Comment(0)
B
11

Instead of 'UTC' use 'zone/city', e.g. TIME_ZONE = 'Europe/Moscow'

A list of possible values ​​can be obtained like this:

import zoneinfo
zoneinfo.available_timezones()
Burnie answered 23/3, 2022 at 18:0 Comment(1)
I tried using a "PDT" timezone, but got a similar issue as the OP. This was really helpful for finding the correct key: "US/Pacific"; however, "UTC" is listed as a valid key. I already had tzdata in my environment (as suggested in the answer). Thanks!Ardine
P
6

This solved this problem

pip install pytz --upgrade
pip install tzdata --upgrade
Phalarope answered 4/10, 2022 at 14:33 Comment(0)
S
0

I got the same error below:

zoneinfo._common.ZoneInfoNotFoundError: 'No time zone found with key America/NewYork'

Because I set 'America/NewYork' to TIME_ZONE as shown below:

# "settings.py"

TIME_ZONE = 'America/NewYork'

So, I set 'America/New_York' to TIME_ZONE as shown below, then the error was solved:

# "settings.py"
                      # ↓ "_" is added
TIME_ZONE = 'America/New_York'
Stank answered 1/6, 2023 at 15:4 Comment(0)
W
0

I tried the methods suggested on this question, like pip install tzdata and changing Time_Zone from 'UTC' to 'GMT+1' and to 'Africa/Lagos' and none of them worked.

Then i activated my virtual environment before installing tzdata and left 'Africa/Lagos' as my timezone and it worked.

Thank you all for the suggestions it was all helpful

Wellfound answered 29/12, 2023 at 13:53 Comment(0)
P
-1

my python's version is 3.11 and Django's version is 4.2.1

I got the same error before:

zoneinfo._common.ZoneInfoNotFoundError: 'No time zone found with key UTC+8'

I tried other methods from this page, such as pip install tzdata or pip install pytz , but none of them worked .

finally, I changed the value of TIME_ZONE from 'UTC+8' to 'Asia/ShangHai', and then the error disappeared!

before:

#settings.py
TIME_ZONE = 'UTC+8'

after:

#settings.py
TIME_ZONE = 'Asia/ShangHai'
Paucity answered 25/7, 2023 at 9:16 Comment(0)
F
-3

for docker container:

docker exec -it your_docker_id bash
bash-5.1# pip install tzdata
Fulfil answered 24/3, 2022 at 14:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.