使用特定状态码在Django中重定向

时间:2018-12-18 16:22:10

标签: python django redirect

如果用户尝试访问页面,我希望将他重定向到登录页面,但状态码为401(未授权)。像这样:

redirect()

但是status函数不像render()函数那样具有<!DOCTYPE html> <html ng-app="plunker"> <head> <meta charset="utf-8" /> <title>AngularJS Plunker</title> <script>document.write('<base href="' + document.location + '" />');</script> <link rel="stylesheet" href="style.css" /> <script data-require="angular.js@1.3.x" src="https://code.angularjs.org/1.3.15/angular.js" data-semver="1.3.15"></script> <script src="app.js"></script> </head> <body ng-controller="MainCtrl"> <p>Branches</p> <ul> <showbranch> <tr> <th> Name </th> </tr> <tr ng-repeat="subBranch in subBranches" <td> {{ f(subBranch.label) }} </td> </showbranch> </ul> </body> </html> 参数。我该怎么办?

2 个答案:

答案 0 :(得分:1)

使用HttpResponse

from django.http import HttpResponse
def yourView(request):    
    return HttpResponse('401 Unauthorized', status=401)

在这里找到解决方案:How do I return a 401 Unauthorized in Django?

答案 1 :(得分:0)

这不是您可以做的事。

在HTTP中,重定向是代码3xx,主要是301和302。这就是浏览器完全知道进行重定向的方式。

如果返回4xx状态代码,浏览器会将其视为错误,而不是重定向。