微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

部署Django Rest Framework后端+ React Web应用程序时URL路由混乱?

如何解决部署Django Rest Framework后端+ React Web应用程序时URL路由混乱?

我目前有一个使用Django Rest Framework构建的后端服务器和一个在React中构建的前端应用程序。当我运行服务器(使用python manage.py runserverwaitress-serve app.wsgi:applicationgunicorn app.wsgi:application)时,无法通过将/ admin附加到url来访问管理页面。出于某种原因,我的React应用的url路由会处理路由并返回404。

project.urls.py

urlpatterns = [
    path('admin/',admin.site.urls),path('api-auth/',include('rest_framework.urls')),path('rest-auth/',include('rest_auth.urls')),path('rest-auth/registration/',include('rest_auth.registration.urls')),path('accounts/',include('allauth.urls')),path('api/',include('items.urls')),re_path('.*',views.FrontendAppView.as_view()),]

views.py

import os
import logging
from django.http import HttpResponse
from django.views.generic import View
from django.conf import settings


class FrontendAppView(View):
    """
    Serves the compiled frontend entry point (only works if you have run `npm run build`).
    """
    index_file_path = os.path.join(settings.FRONTEND_DIR,'build','index.html')

    def get(self,request):
        try:
            with open(self.index_file_path) as f:
                return HttpResponse(f.read())
        except FileNotFoundError:
            logging.exception('Production build of app not found')
            return HttpResponse(
                status=501,)

是什么原因导致此问题?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。