gpt4 book ai didi

django - Django TemplateView 的 xframe_options_exempt

转载 作者:行者123 更新时间:2023-12-02 14:47:50 29 4
gpt4 key购买 nike

我正在尝试将装饰器 @xframe_options_exempt 添加到 django 模板 View 中,但它与

Exception Value: 'dict' object has no attribute 'xframe_options_exempt'

我注意到在 Django 1.9 文档中,装饰器用于带有请求参数的 View ,而我正在使用 TemplateView。

可以这样使用吗?

class MyView(TemplateView):
"""
"""

template_name = 'app/template.html'

from django.views.decorators.clickjacking import xframe_options_exempt

@xframe_options_exempt
def get_context_data(self, **kwargs):

context = {}
context['test'] = 'hello'

return context

基本上我需要将 django 模板 View 嵌入到 iframe 中

最佳答案

当你是decorating class based views时,您应该使用method_decorator。您应该重写将请求作为参数的方法,例如dispatch(将适用于所有请求类型)或get(将适用于 get 请求,但不适用于 post 请求)。正如您所发现的,装饰 get_context_data 将不起作用。

class MyView(TemplateView):

@method_decorator(xframe_options_exempt):
def dispatch(self, *args, **kwargs):
return super(MyView, self).dispatch(*args, **kwargs)

请注意,通过使用 super(),您不必复制 TemplateView 中的代码。

如果您愿意,您可以装饰该类(Django 1.9+)

@method_decorator(xframe_options_exempt, name='dispatch')
class ProtectedView(TemplateView):
template_name = 'secret.html'

关于django - Django TemplateView 的 xframe_options_exempt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37418046/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com