【问题标题】:header data render_to_response to include X-Frame-Options ALLOWALL标头数据 render_to_response 包括 X-Frame-Options ALLOWALL
【发布时间】:2015-04-08 11:52:07
【问题描述】:

我想将X-Frame-Options ALLOWALL 添加到某些页面的标题中(用于某些远程 ajax 调用)。

我的观点很简单,大概是这样的:

def info(request):    
    return render_to_response("info.html",
                               locals(),
                               context_instance=RequestContext(request))

如何修改标题内容?

【问题讨论】:

    标签: python django cors django-cors-headers


    【解决方案1】:

    不要使用 render_to_response(),而是使用 render()

    def info(request):
        data = render(request, 'info.html')
        response = HttpResponse(data)
        response['X-Frame-Options'] = "ALLOWALL"
        return response
    

    还有全局设置。要使用它,请将其添加到中间件: 'django.middleware.clickjacking.XFrameOptionsMiddleware', 并添加到设置 X-Frame-Options = 'ALLOWALL' ,但这通常不是一个好主意,除非您确切知道自己在做什么。

    【讨论】:

    • 您可以通过将第二行更改为response = render(request, 'info.html', locals()),然后删除HttpResponse 行来简化答案。
    猜你喜欢
    • 2013-11-16
    • 1970-01-01
    • 2019-03-15
    • 2017-02-09
    • 2020-07-21
    • 2022-12-18
    • 2015-04-15
    • 2011-02-16
    相关资源
    最近更新 更多