Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to count post by category django

#views.py
from django.db.models import Count
# You should change post in Count function based on your model.
categories = Category.objects.all().annotate(posts_count=Count('post'))

#Then you will have access to number of posts for each category:
for category in categories:
    print(category.posts_count)

#in your template
{% for category in categories %}
      <p>Number of posts: {{category.posts_count}}</p>
{% endfor %}
Comment

PREVIOUS NEXT
Code Example
Python :: python turtle window not responding 
Python :: empty dataframe 
Python :: python request example 
Python :: delete a record by id in flask sqlalchemy 
Python :: open administrator command prompt using python 
Python :: how to check for duplicates in a column in python 
Python :: normalise min max all columns pandas 
Python :: pyhton find dates in weeks 
Python :: # find the common elements in the list. 
Python :: python localhost 
Python :: how to filter out all NaN values in pandas df 
Python :: exact distance math 
Python :: list to string python 
Python :: py exe tkinter 
Python :: plotly scatter markers size 
Python :: python import specific excel sheet 
Python :: mean class accuracy sklearn 
Python :: python log transform column 
Python :: wait() in python tkinter 
Python :: python boxplot legend 
Python :: for each value in column pandas 
Python :: python change base function 
Python :: reset index 
Python :: How to make an simple python client 
Python :: pil image shape 
Python :: python for with iterator index 
Python :: python-binance 
Python :: binary number in python 32 bit 
Python :: finding if user input is lower or upper in python 
Python :: matplotlib boxplot remove outliers 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =