0

I have a Django project, where I am using Redis/Celery to call some API's. I've set up my settings.py file to connect to the Postgres database and when running the Django project locally (with Redis/Celery running locally) the data is stored in the database no problem. When I push the application to Heroku, the data doesn't save to the database.

I'm not sure if this sounds like a problem with the code itself, or the settings of the server causing this (Redis/Celery on Heroku).

I am using a Heroku Postgres Database.

Settings:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': '',
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': 5432,
    }
}

db_from_env = dj_database_url.config(conn_max_age=600)
DATABASES['default'].update(db_from_env)

# The URL of the broker for celery to use
CELERY_BROKER_URL = os.getenv('REDIS_URL', 'redis://localhost:6379/0')
# The URL of the backend you want to use in case you wish to store the result of a task
CELERY_RESULT_BACKEND = os.getenv('REDIS_URL', 'redis://localhost:6379/0')


# The tasks you need to schedule
CELERY_BEAT_SCHEDULE = {
    'add_data': {
        'task': 'comparasion.tasks.add_data_util', # Name of task
        'schedule': 60.0 # Time in seconds
    }
}

Also the procfile:

release: python manage.py migrate
web: gunicorn Soccer.wsgi --log-file -
worker: celery -A Soccer worker --loglevel=info
beat: celery -A Soccer beat --loglevel=info

Not sure if the code provided is enough to help solve this issue.

I've tested this locally and everything worked but as soon as you push to heroku no data saves.

1 Answer 1

0

The settings sample code snippet provided, showcases the configuration for localhost, thus, one solution could be to add a boolean flag in settings to switch between localhost and production instances automatically.

Using the sample code snippet from the Heroku site, below is a demonstration of a django-app, which switches its database instances and host, when executed locally, and when run on a Heroku instance.

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/

# The `DYNO` env var is set on Heroku CI, but it's not a real Heroku app, so we 
# have to also explicitly exclude CI:
# https://devcenter.heroku.com/articles/heroku-ci#immutable-environment- 
# variables

 IS_HEROKU_APP = "DYNO" in os.environ and not "CI" in os.environ

# SECURITY WARNING: don't run with debug turned on in production!
 if not IS_HEROKU_APP:
    DEBUG = True

# SECURITY WARNING: keep the secret key used in production secret!
 SECRET_KEY = os.environ.get(
    "DJANGO_SECRET_KEY",
    default=secrets.token_urlsafe(nbytes=64),
 )

# SECURITY WARNING: don't run with debug turned on in production!
# On Heroku, it's safe to use a wildcard for `ALLOWED_HOSTS``, since the Heroku 
# router performs validation of the Host header in the incoming HTTP request. 
  
# On other platforms you may need to list the expected hostnames explicitly in 
# production to prevent HTTP Host header attacks. See:

# https://docs.djangoproject.com/en/5.0/ref/settings/#std-setting-ALLOWED_HOSTS
if IS_HEROKU_APP:
   ALLOWED_HOSTS = ["*"]
else:
   ALLOWED_HOSTS = [".localhost", "127.0.0.1", "[::1]", "0.0.0.0"]

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

if IS_HEROKU_APP:
   # In production on Heroku the database configuration is derived from the 
   # `DATABASE_URL`
   # environment variable by the dj-database-url package. `DATABASE_URL` will 
   # be set
   # automatically by Heroku when a database addon is attached to your Heroku 
   # app. See:
   # https://devcenter.heroku.com/articles/provisioning-heroku postgres
   # application-config-vars
   # https://github.com/jazzband/dj-database-url
   DATABASES = {
     "default": dj_database_url.config(
        env=os.environ.get("DATABASE_URL"),
        conn_max_age=600,
        conn_health_checks=True,
        ssl_require=True,
      ),
   }

 else:
   # When running locally in development or in CI, a sqlite database file will 
   # be used instead
   # to simplify initial setup. Longer term it's recommended to use Postgres 
   #  locally too.

    DATABASES = {
        "default": env.dj_db_url("DATABASE_URL_LOCAL")
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.