Django,Create-react-app,Heroku

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

我在Django Web应用程序中有3个文件夹。 文件夹如下:包含settings.py(项目)的文件夹,包含models.py(应用程序)的文件夹,以及包含由create-react-app创建的前端反应应用程序的文件夹。

我想构建react前端,将构建工件复制到静态文件夹然后在heroku上运行django应用程序,但是他们使用我当前的文件夹结构几乎不可能完成此过程。 另一种方法是将反应应用程序展平,并在项目的根目录中使用build,src,node_modules,packagejson等等,但这看起来非常糟糕。

settings.py中的一些配置:

STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'static'),
os.path.join(BASE_DIR, 'front-end/build/static')

我在前端本地运行的内容:

npm run build

我从观点回来的内容:

def index(request):

    return HttpResponse(loader.get_template('build/index.html').render())
    #above line returns index.html that is generated by npm run build

如何将上述项目部署到Heroku,以便它可以找到所有静态资源?

django reactjs heroku create-react-app
1个回答
0
投票

您可以使用符号链接将您的react的构建输出放入django目录

例如,如果您的目录结构是这样的:

~/project_root/
~/project_root/django/static/
~/project_root/my_react_app/build

cd到~/project_root/django/static/并运行

ln -s ../../my_react_app/build .

这将创建指向~/project_root/my_react_app/build ~/project_root/django/static/build ~/project_root/my_react_app/build

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