Django rest框架:如果未通过可浏览的API的身份验证,则进行重定向 [英] Django rest framework: redirect if unauthenticated for browsable API
问题描述
如果用户不符合权限标准,我不确定如何实现重定向登录,因为文档指出:在运行视图主体之前,会检查列表中的每个权限."-因此,视图中的压倒性方法是徒劳的.如果有人能指出我正确的方向?谢谢!
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!
推荐答案
Django Rest Framework是用于构建Web API的工具包.它不会渲染充满内容的页面-只是序列化并发送您请求的信息.如果您没有访问服务器上资源的权限,DRF会向您发送 401未经授权
状态或 403禁止
状态响应.
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.
为了将用户重定向到登录页面,您应该在收到 401
状态后在前端进行此操作,这意味着该用户未通过身份验证.
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.
这篇关于Django rest框架:如果未通过可浏览的API的身份验证,则进行重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!