如何在Django中产生303 Http响应?

Ati*_*nch 19 python django rest http

最近几天我们在另一个问题讨论最好以RESTful方式管理随机性; 今天我在Django中玩了一些想法,但发现没有简单的标准方式来返回303响应(也不是300响,顺便说一句),也就是说,里面似乎没有HttpResponseSeeOther django.HTTP或在其他地方.

你知道实现这个目标的方法吗?

gak*_*gak 26

您可以像其他响应一样覆盖HttpResponse:

class HttpResponseSeeOther(HttpResponseRedirect):
    status_code = 303

return HttpResponseSeeOther('/other-url/')
Run Code Online (Sandbox Code Playgroud)

  • 您也可以将其作为带补丁的票证提交. (5认同)

nez*_*roy 20

通用的HttpResponse对象允许您指定所需的任何状态代码:

response = HttpResponse(content="", status=303)
response["Location"] = "http://example.com/redirect/here/"
Run Code Online (Sandbox Code Playgroud)

如果您需要可重复使用的东西,那么Gerald的答案肯定是有效的; 只需创建自己的HttpResponseSeeOther类.Django仅为一些最常见的状态代码提供这些特定类.