2017-08-04 58 views
0

我在後端使用了Angular 4前端和Python Django。 如果我點擊詳細按鈕,我在組件如何在Django中忽略或重定向http請求?

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

運行openDetail方法瀏覽器中打開的URL http://localhost:8000/motor/detail;id=21細節成分。完善。重要的是要知道的是,我只需要id在我的詳細信息組件中與他們一起工作。

但是,如果我刷新頁面,我遇到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和所有數據。

+0

爲什麼你的URL中有';'。這是一個錯字嗎? –

+0

其實不是。它是從一個角教程https://angular.io/guide/router#heroes-list-optionally-selecting-a-hero我希望我理解的內容正確:) - >滾動一點點,它在藍色方框'矩陣URL表示法' –

+1

我建議您使用查詢參數而不是矩陣URL表示法。這是真正的休息標準,它也會容易使用,而不是角度 –

回答

0

您的網址在motors.urls中的映射已針對網址/motor/detail/21正確配置 - 因此它可以正常工作。但它沒有配置爲像/motor/detail;id=21這樣的URL。

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

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