2017-04-30 55 views
5

我在Django Web應用程序中有3個文件夾。這些文件夾如下所示:包含settings.py(項目)的文件夾,包含models.py(應用程序)的文件夾以及包含由create-react-app創建的前端反應應用程序的文件夾。Django,Create-react-app,Heroku

我想構建反應前端,將構建工件複製到一個靜態文件夾中,然後在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,所以它可以找到所有的靜態資源?

+0

一個真正的皮蒂沒有人回答。我和Ruby有類似的問題。 –

+0

@EddeAlmeida for Rails我發現這兩個很好的教程(我想轉移到Django回答這個問題...) - https://www.fullstackreact.com/articles/how-to-get-create-react-app- to-work-with-your-rails-api /和https://medium.com/superhighfives/a-top-shelf-web-stack-rails-5-api-activeadmin-create-react-app-de5481b7ec0b – metakermit

回答

0

您可以使用symlink鏈接,把你的反應的建立輸出到你的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/django/static/build指向~/project_root/my_react_app/build