我正在尝试从get()方法传递查询集以获取上下文数据()。
class StatsView(DetailView):
""" Create statistics pageview """
template_name = 'app/stats.html'
def get(self, request, *args, **kwargs):
queryset = SampleModel.objects.all()
return render(request, self.template_name, context=self.get_context_data(queryset=queryset))
def set_if_not_none(self, mapping, key, value):
if value is not None:
if len(value) != 0:
mapping[key] = value
def get_context_data(self, **kwargs):
qs = kwargs.get('queryset')
# do something
如果你压倒一切
get_context_data()
方法,它可以调用
super()
方法AS
class StatsView(DetailView):
# your code
def get_context_data(self, **kwargs):
data = super(StatsView, self).get_context_data(**kwargs)
data.update({"foo": "bar"})
return data
我想知道一些有关
get()
和
获取上下文数据()
我觉得已经很好地回答了,
When to use get, get_queryset, get_context_data in Django?