我對(duì)使用 Django 很陌生,我一直依賴一些教程將這個(gè)鏈接到 React,但問題是最初(當(dāng)我打開 127.0.0.1:8000 時(shí))React 完美地加載了路由,然后當(dāng)我重新加載頁面 Django 試圖從中解釋路徑urls.py,但顯然找不到它。錯(cuò)誤是:Page not found (404) Using the URLconf defined in memberstack.urls, Django tried these URL patterns, in this order:admin/api/token-auth/core/我希望你能幫助我,在此先感謝我的項(xiàng)目/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貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊
為此,您必須使用包羅萬象,以便 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)
]
我認(rèn)為更好的做法是實(shí)施 Django-Rest-Framework 并單獨(dú)構(gòu)建它們。
添加回答
舉報(bào)
0/150
提交
取消