将 Django 和 React 应用程序部署到 Railway 时出现错误

问题描述 投票:0回答:1

我正在努力将我的 Django 应用程序部署到铁路。该应用程序继续成功部署到 Railway,但是当访问我的铁路应用程序的 URL 时,我继续收到错误:TemplateDoesNotExist at /index.html。由于我将 React 与 Django 一起使用,我了解了在构建静态文件后从 React 提供静态文件的过程,并且在本地运行该项目时,一切都工作正常,只有当我开始部署到 Railway 时,问题才开始出现。作为参考,我存储reactapp 的文件夹称为“reactapp”。以下是我的 manage.py 文件的关键组件:

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',
]

我的主要 urls.py 文件:

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

这是我在铁路部署日志中收到的警告:

?: (staticfiles.W004) STATICFILES_DIRS 设置中的目录“/app/reactapp/build/static”不存在。

我很困惑,因为这个项目在本地运行得很好,但部署到铁路时问题不断出现。感谢任何帮助,谢谢!

编辑:

继续实验后,我认为可能存在问题,因为我使用的是我的github存储库来部署。在我的 github Reactapp 中,构建文件列在 gitignore 中,这意味着它不会显示在存储库中,所以也许这就是项目找不到路径的原因。我应该删除它,还是尝试其他方法?

reactjs django deployment django-templates railway
1个回答
0
投票

我正在尝试部署 django 和 React 项目,连接到 Railway 上的 postgresSQL 数据库,但我有点迷失了。我已经在 Railway 上部署了 django 和 postgres 应用程序,但从未使用过 React。只是想知道您是否可以指导我完成该过程的主要步骤或分享您的存储库(当然如果是公开的)。谢谢!

© www.soinside.com 2019 - 2024. All rights reserved.