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

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


I use an Angular 4 frontend and Python Django in the backend. If I click on the detail button I run the openDetail method in my component

我在后端使用Angular 4前端和Python Django。如果我单击详细信息按钮,则在组件中运行openDetail方法

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

the browser opens the detail component with the URL http://localhost:8000/motor/detail;id=21. Perfect. Important to know is, that I just need the id to work with them in my detail component.

浏览器打开详细信息组件,其URL为http:// localhost:8000 / motor / detail; id = 21。完善。重要的是要知道,我只需要在我的详细信息组件中使用id来处理它们。

But if I refresh the page I run into a 404 error. --> The current path, motor/detail;id=21, didn't match any of these. Well, this is also clear and jumped into the backend.

但是,如果我刷新页面,我会遇到404错误。 - >当前路径,电机/细节; id = 21,与这些中的任何一个都不匹配。嗯,这也很明显,并跳进了后端。

main - urls.py

main - urls.py

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

motors - urls.py

马达 - 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'})),

How can I ignore this call or run a redirect to the index page? I need the id, because the next step, after load the detail page is to load the details by this url http://localhost:8000/motor/detail/21 , it returns JSON with all data.

如何忽略此调用或运行重定向到索引页?我需要id,因为下一步,在加载详细信息页面之后是通过这个url http:// localhost:8000 / motor / detail / 21加载细节,它返回包含所有数据的JSON。

1 个解决方案

#1


0  

Your URL mapping in motors.urls is configured properly for the URL /motor/detail/21 -- so it works. But it's not configured for a URL like /motor/detail;id=21.

您在motors.urls中的URL映射已正确配置为URL / motor / detail / 21 - 因此它可以正常工作。但它没有配置像/ motor / detail这样的URL; id = 21。

If you added another route to motors.url so that /motor/detail;id=21 works -- something like url(r'^detail;id=(?P<id>\d+)$', views.MotorMapping.as_view({'get': 'getById'})), then the URL would return raw JSON, so that's NOT what you want.

如果你添加了另一条路线到motors.url,那么/ motor / detail; id = 21就可以了 - 比如url(r'^ detail; id =(?P \ d +)$',views.MotorMapping.as_view ({'get':'getById'})),然后URL将返回原始JSON,这不是你想要的。

Instead, the actual solution is more complicated -- you want the URL to be routed to your single page app, and not Django, and have Angular take care of loading the data from Django Rest Framework.

相反,实际的解决方案更复杂 - 您希望将URL路由到您的单页应用程序,而不是Django,并让Angular负责从Django Rest Framework加载数据。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2017/08/04/6f9641e9bcace039648f463ff8cccefc.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告