'Get the total count of object with specific status

Trying to get the total count of objects to display on the homepage.

Here's my code

def dashboard(request):
    

    total_issues = Issue.objects.all().count()
    open_issues = Issue.objects.filter(mark_as='Open').count()
    closed_issues = Issue.objects.filter(mark_as='Closed').count()

    context = {'ordered_issues': ordered_issues, 
               'total_issues': total_issues, 
               'open_issues': open_issues,
               'closed_issues': closed_issues}
    return render(request, 'issues/total_issues.html', context)

and my model

class Issue(models.Model):
    MARK_AS = ((True, 'Open'), (False, 'Closed'))
    
    title = models.CharField(max_length=100)
    content = models.TextField()
    date_posted = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    assignee = models.ForeignKey(Profile, on_delete=models.SET_NULL, null=True, blank=True)
    mark_as = models.BooleanField(choices=MARK_AS, default=True)
    
    

    def __str__(self):
        return self.title


    def get_absolute_url(self):
        return reverse('issue-detail', kwargs={'pk': self.pk})

Nothing get outputted

output This code is the template 'issues/total_issues.html

<div class="row">
    <div class="col">
        <div class="col-md">
            <div class="card text-center text-white  mb-3" id="total_issues">
                <div class="card-header">
                    <h5 class="card-title">Total Issues</h5>
                </div>
                <div class="card-body">
                    <h3 class="card-title">{{total_issues}}</h3>
                </div>
            </div>
        </div>
    </div>

    <div class="col">
        <div class="col-md">
            <div class="card text-center text-white  mb-3" id="open_issues">
                <div class="card-header">
                    <h5 class="card-title">Open</h5>
                </div>
                <div class="card-body">
                    <h3 class="card-title">{{open_issues}}</h3>
                </div>
            </div>
        </div>
    </div>

    <div class="col">
        <div class="col-md">
            <div class="card text-center text-white  mb-3" id="closed_issues">
                <div class="card-header">
                    <h5 class="card-title">Closed</h5>
                </div>
                <div class="card-body">
                    <h3 class="card-title">{{closed_issues}}</h3>
                </div>
            </div>
        </div>
    </div>

</div>

What did I do wrong?

ignore this part I'm writing this because apparently there's too much code and not enough detail

ignore this part I'm writing this because apparently there's too much code and not enough detail

ignore this part I'm writing this because apparently there's too much code and not enough detail



Solution 1:[1]

While the values displayed in a drop-down for the mark_as field will be 'Open' and 'Closed', these are not the actual values of the field. Your mark_as field is a BooleanField, so it can only have the values True or False. I'm surprised you don't get an error instead of a blank outputted in the HTML.

Try this:

def dashboard(request):
    
    total_issues = Issue.objects.all().count()
    open_issues = Issue.objects.filter(mark_as=True).count()     # CHANGED
    closed_issues = Issue.objects.filter(mark_as=False).count()  # CHANGED

    context = {'ordered_issues': ordered_issues, 
               'total_issues': total_issues, 
               'open_issues': open_issues,
               'closed_issues': closed_issues}
    return render(request, 'issues/total_issues.html', context)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 raphael