如何在Django中返回未经授权的401? [英] How do I return a 401 Unauthorized in Django?

查看:322
本文介绍了如何在Django中返回未经授权的401?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代替这样做:

res = HttpResponse("Unauthorized")
res.status_code = 401
return res

是否有一种无需每次输入即可完成操作的方法?

Is there a way to do it without typing it every time?

推荐答案

我知道这是一个老版本,但这是Google在 django 401上的最高排名,所以我想指出这一点

I know this is an old one, but it's the top Google result for "django 401", so I thought I'd point this out...

假设您已经导入了 django.http.HttpResponse ,则可以在单行:

Assuming you've already imported django.http.HttpResponse, you can do it in a single line:

return HttpResponse('Unauthorized', status=401)

未授权 字符串是可选的。容易。

The 'Unauthorized' string is optional. Easy.

这篇关于如何在Django中返回未经授权的401?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆