首页 > 解决方案 > Is there a way to modify the HTTP_REFERRER before redirecting in Django?

问题描述

Let's say someone is on www.yahoo.com, and then hits my URL www.mysite.com. Now, in Django, I want to do:

return HttpResponseRedirect('https://www.google.com')

However, when Google receives this request, they will see the HTTP_REFERER was Yahoo, but I want to modify the referer so it shows ‘www.mysite.com’. Can this be done?

标签: pythondjangoredirecthttp-status-code-301

解决方案


您应该能够执行以下操作:

resp = HttpResponseRedirect('https://www.google.com')
resp['HTTP_REFERER'] = 'www.mysite.com'
return resp

这是因为HttpResponseRedirect是 的子类HttpResponse,因此您可以像设置标题字段一样设置HttpResponse


推荐阅读