Django,Create-react-app,Heroku

cat*_*234 5 django heroku reactjs create-react-app

我在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')
Run Code Online (Sandbox Code Playgroud)

)

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

npm run build
Run Code Online (Sandbox Code Playgroud)

我从观点回来的内容:

def index(request):

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

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

小智 0

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

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

~/project_root/
~/project_root/django/static/
~/project_root/my_react_app/build
Run Code Online (Sandbox Code Playgroud)

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

ln -s ../../my_react_app/build .
Run Code Online (Sandbox Code Playgroud)

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