我對使用 Django 很陌生,我一直依賴一些教程將這個鏈接到 React,但問題是最初(當我打開 127.0.0.1:8000 時)React 完美地加載了路由,然后當我重新加載頁面 Django 試圖從中解釋路徑urls.py,但顯然找不到它。錯誤是:Page not found (404) Using the URLconf defined in memberstack.urls, Django tried these URL patterns, in this order:admin/api/token-auth/core/我希望你能幫助我,在此先感謝我的項目/urls.pyfrom django.contrib import adminfrom django.urls import path, includefrom rest_framework_jwt.views import obtain_jwt_tokenfrom frontend.views import indexurlpatterns = [ path('', include('frontend.urls')), path('admin/', admin.site.urls), path('api/token-auth/', obtain_jwt_token), path('core/', include('core.urls')),]前端/urls.pyfrom django.urls import pathfrom . import viewsurlpatterns = [ path('', views.index),]前端/views.pyfrom django.shortcuts import renderdef index(request): return render(request, 'frontend/index.html')
1 回答

慕后森
TA貢獻1802條經驗 獲得超5個贊
為此,您必須使用包羅萬象,以便 React 處理所有路由,而不是那里的 django。
from django.urls import path, re_path
from . import views
urlpatterns = [
path('', include('frontend.urls')),
path('admin/', admin.site.urls),
path('api/token-auth/', obtain_jwt_token),
path('core/', include('core.urls')),
re_path(r'^(?:.*)/?$', include('frontend.urls')),
]
或者
urlpatterns = [
path('', views.index),
re_path(r'^(?:.*)/?$', views.index)
]
我認為更好的做法是實施 Django-Rest-Framework 并單獨構建它們。
添加回答
舉報
0/150
提交
取消