简体   繁体   English

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

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

I use an Angular 4 frontend and Python Django in the backend. 我在后端使用Angular 4前端和Python Django。 If I click on the detail button I run the openDetail method in my component 如果单击详细信息按钮, openDetail在组件中运行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 . 浏览器将打开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. 重要的是要知道,我只需要id即可在我的detail组件中使用它们。

But if I refresh the page I run into a 404 error. 但是,如果刷新页面,则会遇到404错误。 --> The current path, motor/detail;id=21, didn't match any of these. -> The current path, motor/detail;id=21, didn't match any of these. Well, this is also clear and jumped into the backend. 好吧,这也很清楚,并跳入了后端。

main - urls.py 主要-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。

Your URL mapping in motors.urls is configured properly for the URL /motor/detail/21 -- so it works. 在motors.urls中为URL /motor/detail/21正确配置了URL映射-这样就可以了。 But it's not configured for a URL like /motor/detail;id=21 . 但未为/motor/detail;id=21这样的URL配置它。

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<id>\\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加载数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM