我想添加http标头,但它不起作用。 我正试图通过使用打印来测试求解,但它似乎没有什么
这是我的代码,但不起作用:
class MyRedirectView(RedirectView):
def head(self, *args, **kwargs):
response = HttpResponse()
response['X-Robots-Tag'] = 'noindex'
print('TEST')
return response
答案 0 :(得分:1)
您在此处所做的是覆盖head
方法。仅在对您的URL进行HEAD类型的HTTP请求时使用。您应该覆盖get
方法,或者更好地覆盖调度方法。
class MyRedirectView(RedirectView):
def dispatch(self, *args, **kwargs):
response = super(MyRedirectView,self).dispatch(*args, **kwargs)
response['X-Robots-Tag'] = 'noindex'
print('TEST LOL')
return response