簡體   English   中英

如何在Django中忽略或重定向http請求?

[英]How to ignore or redirect a http request in Django?

我在后端使用Angular 4前端和Python Django。 如果單擊詳細信息按鈕, openDetail在組件中運行openDetail方法

openDetail(id:number){
    this.router.navigate(['/motor/detail', {"id": id}])
  }

瀏覽器將打開URL為http://localhost:8000/motor/detail;id=21的詳細信息組件。 完善。 重要的是要知道,我只需要id即可在我的detail組件中使用它們。

但是,如果刷新頁面,則會遇到404錯誤。 -> The current path, motor/detail;id=21, didn't match any of these. 好吧,這也很清楚,並跳入了后端。

主要-urls.py

#... some other urls...
url(r'^motor/', include('motorControll.urls')),

馬達-urls.py

url(r'^$', views.index, name="index"),
url(r'^overview/$', views.MotorMapping.as_view({'get': 'getAllData'})),
url(r'^detail/$', views.index, name="index"),
url(r'^detail/(?P<id>\d+)/$', views.MotorMapping.as_view({'get': 'getById'})),
url(r'^detail/save/$', views.MotorMapping.as_view({'post': 'save'})),

如何忽略此調用或運行重定向到索引頁面? 我需要id,因為加載詳細信息頁面后的下一步是通過此URL http://localhost:8000/motor/detail/21加載詳細信息,它將返回包含所有數據的JSON。

在motors.urls中為URL /motor/detail/21正確配置了URL映射-這樣就可以了。 但未為/motor/detail;id=21這樣的URL配置它。

如果您向motors.url添加了另一條路由,以便/motor/detail;id=21起作用-類似於url(r'^detail;id=(?P<id>\\d+)$', views.MotorMapping.as_view({'get': 'getById'})),然后該URL將返回原始JSON,所以這不是您想要的。

相反,實際的解決方案更加復雜-您希望將URL路由到您的單頁應用程序,而不是Django,並且讓Angular負責從Django Rest Framework加載數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM