skip to Main Content

I want to use Redis through docker for the cache but got this error.

django.core.cache.backends.base.InvalidCacheBackendError: Could not find backend 'django.core.cache.backends.redis.RedisCache': No module named 'django.core.cache.backends.redis'

My cache settings are this.

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.redis.RedisCache',
        'LOCATION': 'redis://127.0.0.1:6379',
    }
}

I took them from Django documentation.

I used this command to start a Redis instance in docker.

docker run --name some-redis -d redis

2

Answers


  1. Django introduced the Redis backend in version 4.0.
    https://docs.djangoproject.com/en/4.1/topics/cache/#redis

    Make sure the the Django version in the docker container is at least 4.0.

    Or if you don’t want to upgrade Django, you can use packages such as django-redis.

    Login or Signup to reply.
  2. I think the issue comes from the line 'LOCATION': 'redis://127.0.0.1:6379' which do not match the name you’ve given to the redis container in your docker network.

    Indeed, the command docker run --name some-redis -d redis assigns some-redis to the container name, so yo have to refer to your Redis instance with this name in your Python code.

    In a nutshell, you have to replace 'LOCATION': 'redis://127.0.0.1:6379' by 'LOCATION': 'redis://some-redis:6379' in the above code.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search