1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Heroku changing base_dir to 'app' in Django & React app

Discussão em 'Python' iniciado por Stack, Setembro 12, 2024.

  1. Stack

    Stack Membro Participativo

    I have a django/react app with the file structure

    funcon(main directory // django project - contains manage.py)

    -funcon (sub directory // django)
    -core (sub directory // django)
    -funcon-frontend (subdir // react)


    When I run the project locally, everything works great. The react project is the first thing I see when I go to localhost:8000, and the auth requests work between it and the api. When I deploy to heroku, for some reason it seems like it converts my BASE_DIR to app/funcon-frontend and I can't find why it's doing that or how to change it. I am just trying to deploy the react + django app together.

    These are the build logs from heroku

    Frontend build dir: /app/funcon-frontend/build
    /app/.heroku/python/lib/python3.11/site-packages/django/core/handlers/base.py:61:
    UserWarning: No directory at: /app/staticfiles/
    /app/.heroku/python/lib/python3.11/site-packages/django/core/handlers/base.py:61:
    UserWarning: No directory at: /app/funcon-frontend/build/
    mw_instance = middleware(adapted_handler)
    app/.heroku/python/lib/python3.11/site-packages/django/core/handlers/base.py:61:
    UserWarning: No directory at: /app/staticfiles/
    mw_instance = middleware(adapted_handler)
    /app/.heroku/python/lib/python3.11/site-packages/django/core/handlers/base.py:61:
    UserWarning: No directory at: /app/funcon-frontend/build/
    mw_instance = middleware(adapted_handler)


    I use whitenoise (in my requirements.txt)

    settings.py


    BASE_DIR = Path(__file__).resolve().parent.parent
    dotenv_path = os.path.join(BASE_DIR, '.env')
    load_dotenv(dotenv_path)

    TEMPLATES = [
    {
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, 'funcon-frontend', 'build')],
    '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',
    ],
    },
    },
    ]

    STATIC_URL = '/static/'
    STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
    STATICFILES_DIRS = []
    frontend_static = os.path.join(BASE_DIR, 'funcon-frontend', 'build', 'static')
    if os.path.exists(frontend_static):
    STATICFILES_DIRS.append(frontend_static)

    STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

    WHITENOISE_ROOT = os.path.join(BASE_DIR, 'funcon-frontend', 'build')

    django_heroku.settings(locals(), staticfiles=False)


    I have a package.json at my root directory, otherwise heroku doesn't recognize the Nodejs app and it goes

    "name": "funcon",
    "version": "1.0.0",
    "scripts": {
    "heroku-postbuild": "echo 'Starting build process' && cd funcon-frontend && echo 'In frontend directory' && npm install && echo 'Dependencies installed' && npm run build && echo 'Build complete' && ls -la"
    },
    "engines": {
    "node": "14.x"
    },
    "cacheDirectories": ["funcon-frontend/node_modules"]
    }


    here is my procfile

    web: gunicorn funcon.wsgi --log-file - release: python manage.py migrate

    Continue reading...

Compartilhe esta Página