Django rest framework: redirect if unauthenticated

2019-07-20 13:53发布

I'm not sure how to implement a redirect to login if the user is doesn't meet the permissions criteria, as the documentation states: "Before running the main body of the view each permission in the list is checked." - so overriding methods in the view is fruitless. If anyone could point me in the right direction? Thanks!

1条回答
beautiful°
2楼-- · 2019-07-20 14:10

Django Rest Framework is a toolkit to build web APIs. It doesn't render pages filled with content - it just serializes and sends the information which you requested. And in case you don't have the permission to access a resource on server, DRF sends you either a 401 Unauthorized status, or a 403 Forbidden status responses.

In order to redirect a user to the login page, you should do this in the frontend, after receiving a 401 status, which means that the user is not authenticated.

查看更多
登录 后发表回答