我不确定标题中的措辞是否合适,所以请在下面查看我对我的疑问的描述。
在" def get_queryset(self)"中,我过滤了结果,然后将此过滤结果传递给queryset。
然后在" def get_context_data(self,** kwargs):"我想使用" get_queryset"的过滤结果做一个计算。但问题是我得到的最终结果并没有应用过滤器。
所以我怀疑" get_queryset"在get_context_data中没有从" def get_queryset"中获取值。
请提前帮助纠正我。
views.py
class InputFormView(FormView):
template_name = 'inputform.html'
form_class = InputForm
def get_success_url(self):
return ''.join(
[
reverse('result'),
'?company=',self.request.POST.get('company'), <--do I need to change "POST" into "USER"?
'®ion=',self.request.POST.get('region')
]
)
class ResultView(ListView):
context_object_name = 'result_list'
template_name = 'result_list.html'
model = Result
def get_context_data(self, **kwargs):
context = super(ResultView, self).get_context_data(**kwargs)
context["sales"] = self.get_queryset().aggregate(Sum('sales'))
return context
def get_queryset(self):
if self.request.method == 'POST':
form = InputForm(self.request.POST)
if form.is_valid():
region = form.cleaned_data['region']
queryset=Result.objects.filter(region=region)
return queryset 《--if this queryset result go to the "get_queryset()" of "def get_context_data"--》?
return Result.objects.all()
根据Danniel的建议测试views.py
class InputFormView(FormView):
template_name = 'inputform.html'
form_class = InputForm
def get_success_url(self):
return ''.join(
[
reverse('result'),
'?company=',self.request.POST.get('company'), <--do I need to change "POST" into "USER"?
'®ion=',self.request.POST.get('region')
]
)
class ResultView(ListView):
context_object_name = 'result_list'
template_name = 'result_list.html'
model = Result
def get_context_data(self, **kwargs):
context = super(ResultView, self).get_context_data(**kwargs)
context["sales"] = self.get_queryset().aggregate(Sum('sales'))
return context
def get_queryset(self):
return Result.objects.filter(region='EU') <--- there exists already "EU" in the database--->
测试views.py 上的 回溯
result models.py File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\core\handlers\base.py" in get_response
164. response = response.render()
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\template\response.py" in render
158. self.content = self.rendered_content
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\template\response.py" in rendered_content
135. content = template.render(context, self._request)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\template\backends\django.py" in render
72. context = make_context(context, request)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\template\context.py" in make_context
272. context.push(original_context)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\template\context.py" in push
55. return ContextDict(self, *args, **kwargs)
File "C:\Python27\lib\site-packages\django-1.8.3-py2.7.egg\django\template\context.py" in __init__
20. super(ContextDict, self).__init__(*args, **kwargs)
Exception Type: TypeError at /dupont_list/
Exception Value: cannot convert dictionary update sequence element #0 to a sequence
class Result(models.Model):
company=models.CharField(max_length=10,blank=True)
region=models.CharField(max_length=10,blank=True)
sales=models.DecimalField(max_digits=12,decimal_places=3,blank=True,null=True)