亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Django 和 React 進行路由

使用 Django 和 React 進行路由

www說 2023-03-16 10:55:55
我對使用 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 并單獨構建它們。


查看完整回答
反對 回復 2023-03-16
  • 1 回答
  • 0 關注
  • 104 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號