Python Django中的返回位置标头

Python Django中的返回位置标头,python,django,django-views,Python,Django,Django Views,我正在编写一个缩短URL的API,我必须返回以下响应 request: GET /:shorten_url Content-Type: "application/json" Expected Response: 302 response with the location header pointing to the shortened URL HTTP/1.1 302 Found Location: http://www.example.com # original u

我正在编写一个缩短URL的API,我必须返回以下响应

request:
GET /:shorten_url
Content-Type: "application/json"

Expected Response:

302 response with the location header pointing to the shortened URL
HTTP/1.1 302 Found
Location: http://www.example.com # original url
我试过:

# shortcode is the shorten url of original url
def get(self, request, shortcode):
    if shortcode:
        try:
            shortUrl = UrlShort.objects.get(shortcode=shortcode)
            originalUrl = shortUrl.url
            response = HttpResponse(status=status.HTTP_302_FOUND)
            response['Location'] = shortUrl.url
            return response

        except UrlShort.DoesNotExist:
            raise Http404()

我没有得到带有位置头的302状态码,而是被重定向到带有状态码200的
url
。我的代码出了什么问题?

有一个特殊的类用于“找到”重定向,您可以使用-redirect:

...
return HttpResponseRedirect(shortUrl.url)

我想获取位置标题而不是重定向请查看我期望的响应,但
位置
标题实际上指向浏览器执行重定向:“要请求web浏览器加载不同的网页(URL重定向)。在这种情况下,位置标题应以3xx的HTTP状态代码发送”,所以你是设置代码302和位置标题-你实际上应该被重定向。你是否粘贴了
get
方法的全部代码以供查看?对于
shortcode
equal
None
场景,您是否有其他部分?