【问题标题】:How to produce a 303 Http Response in Django?如何在 Django 中生成 303 Http 响应?
【发布时间】:2010-09-29 08:12:47
【问题描述】:

前几天我们在another question 讨论了以 RESTful 方式管理随机性的最佳方法;今天我在Django中玩了一些想法,结果发现没有简单的标准方法可以返回303响应(也不是300,顺便说一句),也就是说,里面似乎不存在HttpResponseSeeOther django.HTTP 或其他地方。

您知道实现此目的的任何方法吗?

【问题讨论】:

    标签: python django http rest


    【解决方案1】:

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

    class HttpResponseSeeOther(HttpResponseRedirect):
        status_code = 303
    
    return HttpResponseSeeOther('/other-url/')
    

    【讨论】:

    • 您也可以将其归档为带有补丁的票证。
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-22
      • 1970-01-01
      • 2016-01-23
      • 2013-03-03
      • 2014-06-22
      • 2017-02-08
      相关资源
      最近更新 更多