覆盖 get_context_data() 在子视图中不起作用
Overriding get_context_data() is not working in child view
我试图在基于 class 的子视图中覆盖 get_context_data()
以将更多上下文数据发送到模板,但它不起作用。作为示例,我发送了一个测试变量,但它没有在模板中呈现。
class ProductList(LoginRequiredMixin, View):
template_name = 'product/product_scroll.html'
def get(self, request, *args, **kwargs):
#...
return render(request, self.template_name, data)
class Feed(ProductList):
template_name = "product/feed.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['test'] = 'sometestinfo'
return context
但在模板中:
<p> Prueba: {{test}} </p>
为空。
它不起作用,因为您覆盖了 get
。因此,视图的整个内置功能 - 包括调用 get_context_data
- 都被绕过了。您几乎不需要定义 get 或 post 方法。
我试图在基于 class 的子视图中覆盖 get_context_data()
以将更多上下文数据发送到模板,但它不起作用。作为示例,我发送了一个测试变量,但它没有在模板中呈现。
class ProductList(LoginRequiredMixin, View):
template_name = 'product/product_scroll.html'
def get(self, request, *args, **kwargs):
#...
return render(request, self.template_name, data)
class Feed(ProductList):
template_name = "product/feed.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['test'] = 'sometestinfo'
return context
但在模板中:
<p> Prueba: {{test}} </p>
为空。
它不起作用,因为您覆盖了 get
。因此,视图的整个内置功能 - 包括调用 get_context_data
- 都被绕过了。您几乎不需要定义 get 或 post 方法。