我使用的是一个带有DRF后端的React前端,我在同一个URL上托管了这两个后端。我得到了前端的静态产品构建,我把它放在静态文件夹下,当一个人转到主contoso.com时,他们被重定向到contoso.com/static/index.html,在那里静态构建被服务,不同的URL可以正确地使用HashRouter和重新加载页面来访问,etc按预期工作,但是链接像contoso.com/static/index.html#home、contoso.com/static/index.html#menu等。我希望在主URL下面只有contoso.com/#home,contoso.com/#menu。这方面的API后端存在于contoso.com/api下。
附加信息:在IIS v10上托管的与带有静态文件夹的v10作为公共
发布于 2021-08-22 13:53:58
首先,像这样从settings.py中修改模板代码
'DIRS': [
os.path.join(BASE_DIR, '/static') #If index.html file is in static folder.
],然后在project.urls.py里
from django.urls import path
from django.views.generic import TemplateView
urlpatterns = [
#...
path('', TemplateView.as_view(template_name='index.html')),
]现在,拔掉您在注释中共享的前一个视图,并使用这个URL从Reactive前端的构建中呈现index.html。
那这个就会按照你的意愿运作。example.com/#home
发布于 2021-08-22 13:40:46
能够在基本web.config文件的另一个路径下分离api (DRF)部分,然后使用index.html设置默认文档。因此,我认为最好的选择是将其移动到生产服务器上,以确保安全(在本例中为IIS)。
<handlers>
<add name="Django FastCGI"
path="api"
verb="*"
requireAccess="Script" />
</handlers>
<staticContent>
<mimeMap fileExtension=".*" mimeType="text/html" />
</staticContent>
<defaultDocument>
<files>
<clear />
<add value="index.html" />
</files>
</defaultDocument>https://stackoverflow.com/questions/68881004
复制相似问题