部署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 (将#修改为@)