我在后端使用Angular 4前端和Python Django。
如果我单击详细信息按钮,则在组件中运行openDetail
方法
openDetail(id:number){
this.router.navigate(['/motor/detail', {"id": id}])
}
浏览器打开包含网址http://localhost:8000/motor/detail;id=21
的详细信息组件。完善。重要的是,我只需要在我的详细信息组件中使用id来处理它们。
但如果我刷新页面,我会遇到404错误。 - > The current path, motor/detail;id=21, didn't match any of these.
嗯,这也很清楚并跳进了后端。
main - urls.py
#... some other urls...
url(r'^motor/', include('motorControll.urls')),
motors - 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 :(得分:0)
您在网址/motor/detail/21
中正确配置了motors.urls中的网址映射 - 因此可行。但是,它没有针对/motor/detail;id=21
等网址进行配置。
如果您向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加载数据。