【发布时间】:2019-05-12 09:33:26
【问题描述】:
我正在使用单个 python 后端为 2 个 React 应用程序提供服务。现在,我使用 HttpsREsponse 为 index.html 提供服务,并且我已将 build/static 包含在 STATICFILES_DIRS 中。因为这些文件前面有哈希,所以它们可能会混淆。但是我需要一种方法来提供build 中的所有这些文件,例如manifes.json 或favicon.ico。为所有人创建View 似乎是个糟糕的主意。我很乐意将app1/build 作为static/app1 和app2/build 作为static/app2 服务。
我搜索了 Django 文档,发现了如何从多个位置收集静态数据以及有关在多个 URL 上托管它们的任何信息。
这是我现在的设置:
# settings.py
STATICFILES_DIRS = [
os.path.join(APP1_DIR, 'build', 'static'),
os.path.join(APP2_DIR, 'build', 'static')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
# urls.py
urlpatterns = [
url(r'^app1/manifest.json$', getStaticFileView(app='app1', 'manifest.json').as_view()),
url(r'^app1', getStaticFileView(app='app1', 'index.html').as_view()),
url(r'^app2/manifest.json$', getStaticFileView(app='app2', 'manifest.json').as_view()),
url(r'^app2', getStaticFileView(app='app2', 'index.html').as_view()),
]
我正在寻找这样的东西:
# settings.py
STATICFILES_MAPPING = [
(os.path.join(APP1_DIR, 'build', 'static'), 'static/app1'),
(os.path.join(APP2_DIR, 'build', 'static'), 'static/app2')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
# urls.py
urlpatterns = [
url(r'^app1', getStaticFileView(app='app1', 'index.html').as_view()),
url(r'^app2', getStaticFileView(app='app2', 'index.html').as_view()),
]
感谢您的任何想法或指示。
编辑:我使用静态服务器提供来自/static 的所有静态文件,但我需要告诉 django 创建正确的/static 文件结构。不要只是把所有东西都扔到/static 文件夹中,而是把东西放在/static/app1 和/static/app2 中
【问题讨论】:
-
你应该使用外部服务来服务静态文件,django 不是作为静态文件服务器设计的;有关如何设置用于生产的静态文件服务的信息,请参阅 the documentation。
标签: python django reactjs django-staticfiles