0

I'm struggling to deploy my django app to railway. The app continues to successfully deploy to Railway, however when visiting the URL of my railway app, I continue to get the error: TemplateDoesNotExist at /index.html. Since I'm using react with Django, I understand the process of serving the static files from react after they are built, and when running this project locally, everything has been working fine, the issue only started once I began to deploy to Railway. For reference, the folder where I've stored my reactapp is called 'reactapp'. Here are the key components of my manage.py file:

ALLOWED_HOSTS = ["myprojecturl.railway.app", "127.0.0.1"]

CORS_ALLOWED_ORIGINS = [
    "http://127.0.0.1:8000/home/",
    "myprojecturl.railway.app"
]


TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'api/templates/api'),
            BASE_DIR / 'reactapp/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_ROOT = os.path.join(BASE_DIR, 'staticfiles')       
STATIC_URL = 'static/'

STATICFILES_DIRS = [
    BASE_DIR / 'reactapp/build/static',
]

My main urls.py file:

path('', TemplateView.as_view(template_name='index.html'))

Here was the warning I received in my deployment logs from Railway:

?: (staticfiles.W004) The directory '/app/reactapp/build/static' in the STATICFILES_DIRS setting does not exist.

I'm confused because this project works fully well locally, but the problems keep arising when deploying to Railway. Any help is appreciated, Thanks!

Edit:

After continuing to experiment, I think there may be an issue since I'm using my github repository to deploy. In my github reactapp, the build file is listed in git ignore, meaning it doesn't show up in the repo, so maybe this is why the project cannot find the path. Should I remove this, or try something else?

Andrew
  • 31
  • 5
  • Have you run `collectstatic` on your deployment target? – michjnich Aug 31 '23 at 07:12
  • @michjnich Yes, I ran python3 manage.py collectstatic. I believe it generated a new directory called 'staticfiles' which is in my project now. Do i need to somehow connect my URL patterns to this directory? – Andrew Aug 31 '23 at 07:20
  • `staticfiles` is there because that's what you've defined as `STATIC_ROOT`. For a production server you should serve all your static files from there, yes. Often this is done at the server (eg nginx, apache) level. Your url patterns should not have any static directories in them at all. – michjnich Aug 31 '23 at 13:05
  • @michjnich So what should I do with the build folder? This seems to be causing the problem where it cannot find index.html, because in my github repo there is no build subdirectory? – Andrew Aug 31 '23 at 15:48
  • @michjnich The application successfully deploys once I remove the build file from git ignore. Is this generally a bad practice, and if so what should I have done instead? – Andrew Aug 31 '23 at 16:11

0 Answers0