Add request header before redirection

前端 未结 1 1653
攒了一身酷
攒了一身酷 2020-12-19 14:35

I am retrofitting a Django web app for a new client. To this end I have added a url pattern that redirects requests from new client to old url patterns.

from:-

1条回答
  •  借酒劲吻你
    2020-12-19 15:01

    Django FBV's should return an HTTPResponse object (or subclass thereof). The Django shorcut redirect returns HttpResponseRedirect which is a subclass of HTTPResponse. This means we can set the headers for redirect() the way we will set headers for a typical HTTPResponse object. We can do that like so:

    def my_view(request):
        response = redirect('http://www.gamefaqs.com')
        # Set 'Test' header and then delete
        response['Test'] = 'Test'
        del response['Test']
        # Set 'Test Header' header
        response['Test Header'] = 'Test Header'
        return response
    

    Relevant docs here and here.

    0 讨论(0)
提交回复
热议问题