自定义计算返回'NoneType'对象是不可调用的 Django



我正在尝试为我的系统做一些计算,我需要获得superuser的所有位置计数,就像一样

location = LocationData.objects.filter(email=self.request.user.email).count()

然后我需要计算该用户的所有许可证,就像一样

count = MyUser.objects.filter(location_count=self.request.user.email).count()

并且我需要检查location==count和到*的位置是否有一些价格,

if count == location:
context['social'] = location * FRISTPRICE

当我得到价格时,我需要在模板上显示它。

完整视图为

class AdminDashboard(TemplateView):
"""
"""
template_name = 'administration/admin.html'
@cached_property
def get_context_data(self, **kwargs):
context = super(AdminDashboard, self).get_context_data(**kwargs)
user = MyUser.objects.get(pk=self.request.user.pk)
# check if user is superuser if not don't include him
if user.is_superuser:
# check how much locations does user have
location = LocationData.objects.filter(email=self.request.user.email).count()
# check how much user have licences payed for
count = MyUser.objects.filter(location_count=self.request.user.email).count()
# if count is == to location then the location is proper
# so count * package = application sales
if count == location:
context['first_package'] = location * FIRSTPRICE
if count == location:
context['second_package'] = location * SECONDPRICE
if count == location:
context['third_package'] = location * THIRDPRICE
return context

完整的错误在这里,我的第一个猜测是上下文无法返回,而且这个计算不好,所以有人能帮我理解为什么我会出现这个错误吗,谢谢。

如果用户不是超级用户,则get_context_data不返回任何内容。取消返回上下文行以修复它:
@cached_property
def get_context_data(self, **kwargs):
context = super(AdminDashboard, self).get_context_data(**kwargs)
user = MyUser.objects.get(pk=self.request.user.pk)
# check if user is superuser if not don't include him
if user.is_superuser:
# check how much locations does user have
location = LocationData.objects.filter(email=self.request.user.email).count()
# check how much user have licences payed for
count = MyUser.objects.filter(location_count=self.request.user.email).count()
# if count is == to location then the location is proper
# so count * package = application sales
if count == location:
context['first_package'] = location * FIRSTPRICE
if count == location:
context['second_package'] = location * SECONDPRICE
if count == location:
context['third_package'] = location * THIRDPRICE
return context

最新更新