在RedirectVi中添加自定义http头

2024-10-03 06:31:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我想添加http头,但它不起作用。 我试着用印刷品来测试解题,但似乎什么也没有

这是我的代码,但不起作用:

class MyRedirectView(RedirectView):

    def head(self, *args, **kwargs):
        response = HttpResponse()
        response['X-Robots-Tag'] = 'noindex'
        print('TEST')
        return response

Tags: 代码selfhttpresponsedefargsheadkwargs
1条回答
网友
1楼 · 发布于 2024-10-03 06:31:52

这里所做的是重写head方法。它只在对您的url发出HEAD类型的HTTP请求时使用。您应该重写get方法,或者更好地替代dispatch方法。在

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

相关问题 更多 >