自定义计算返回'NoneType'对象不可调用django

问题描述 投票:0回答:1

我正在尝试为我的系统做一些计算,我需要得到所有位置计数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

并且完整的错误是HERE,我的第一个猜测是上下文无法返回并且这个计算不好,所以有人可以帮助我理解为什么我得到这个错误,谢谢。

django
1个回答
2
投票

如果用户不是超级用户,get_context_data不会返回任何内容。 Unindent返回上下文行来修复它:

@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
© www.soinside.com 2019 - 2024. All rights reserved.