- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将装饰器 @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/
我正在尝试将装饰器 @xframe_options_exempt 添加到 django 模板 View 中,但它与 Exception Value: 'dict' object has no attr
我希望通过 iframe 从另一个域加载我的 View 页面,因此按照 Django 文档,我将装饰器 @xframe_options_exempt 添加到我的 View 中。但是在iframe中加载
我是一名优秀的程序员,十分优秀!