gpt4 book ai didi

django - 使用 create-react-app 自定义静态文件路径?

转载 作者:行者123 更新时间:2023-12-04 09:25:44 25 4
gpt4 key购买 nike

我一直在研究 Django-React 应用程序,今天终于将它推送到 Heroku。我使用在 DjangoCon-talk 中看到的一种技术从 Django 模板 View 为我的 SPA 提供服务。我手动构建 react-app,然后针对 index.html .我也在使用 AWS S3 来存储我的静态文件,这就是我卡住的地方。
当我推送到 Heroku 时,collectstatic将我的静态文件添加到 AWS。但是,我访问这些静态文件的唯一方法是推送应用程序,从 AWS 复制静态 URL,将它们粘贴到我的 index.html 中的样式表/脚本标签中。然后再次插入。
有没有办法在运行时动态创建这些 npm run build ?
类似 https://my-bucket.s3.amazonaws.com/static/js/{my_js_file}.chunk.js ?
如果这是不可能的,是否有其他解决方案?提示将不胜感激。

最佳答案

我通过捕获所有以“静态”开头的路径并将它们重定向到存储桶来解决这个问题。
以下是在我的 urls.py 中使用“静态”重定向路径并将其余部分转过来使用react。对于其他情况,您可以添加您的 api 地址等:

urlpatterns += [
re_path('^static', redirect_static),
re_path('.*', render_react),
]
以下是 redirect_static 函数/ View :
def redirect_static(request):
return redirect(f"https://<bucket_address>{request.path}")

关于django - 使用 create-react-app 自定义静态文件路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63020877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com