Q:

How to get limited amount of recent posts in django

I am a DJANGO beginner and I am making a biography app as my first project. I want my homepage to show a limited number of recent posts. I am providing my Django files so you'll look up and help me with this problem. Thanks
#views.py
from django.views.generic import TemplateView, ListView, DetailView
from .models import About

# Create your views here.

class Home(ListView):
    """docstring for Home"""
    template_name = 'home.html'
    model = About
    recents = About.objects.all()
    for recent in recents:
        if recent.was_published_recently():
            print (recent.id)

class BiographyDetail(DetailView):
    """docstring for BiographyDetail"""
    model = About
    template_name = 'details.html'

class About(TemplateView):
    """docstring for BiographyDetail"""

    template_name = 'about.html'
    model = About

class Contacts(TemplateView):
    """docstring for BiographyDetail"""

    template_name = 'contact.html'
    model = About

class Projects(TemplateView):
    """docstring for BiographyDetail"""

    template_name = 'projects.html'
    model = About

class Search(TemplateView):
    """docstring for BiographyDetail"""

    template_name = 'search.html'
    model = About

#models.py I have defined a function was_published_recently which tells whether any posts was published in that certain interval as you can figure that out by its name
from django.db import models
from django.utils import timezone
import datetime
# Create your models here.

TYPE = (
    ('politician','POLITICIAN'),
    ('poet','POET'),
    ('author','AUTHOR'),
    ('actor','ACTOR'),
    )

class About(models.Model):
    name = models.CharField(max_length = 200, default='')
    quote = models.CharField(max_length = 200, default='')
    born = models.DateField(verbose_name = 'Born on', null = True)
    born_at = models.CharField(max_length = 200, default='')
    died = models.DateField(verbose_name = 'Died on', null = True)
    died_at = models.CharField(max_length = 200, default='')
    main = models.TextField(default='')
    category = models.CharField(choices = TYPE , default = 'poet', max_length = 10)
    image = models.ImageField(blank=True, default='', upload_to='static')
    created_on = models.DateTimeField(auto_now_add = True)
    updated_on = models.DateTimeField(auto_now = True)

    def __str__(self):
        return self.name

    def was_published_recently(self):
        return self.created_on >= timezone.now() - datetime.timedelta(days=15)

home.html
{% extends 'base.html' %}

{% block content %}
    <!-- Header Starts -->
    <header style="background-color: black;">
        <div class="container">

          <nav class="navbar navbar-expand-lg" >

            <a class="navbar-brand" href="{% url 'home' %}" style="color: white; font-family: 'Vollkorn', serif; font-weight: 800;">J<span style="color: red;">O</span>URNEY</a>

            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation"> 
                <a href="#">
                    <svg class="bi bi-filter-left" width="1.5em" height="1.5em" viewBox="0 0 16 16" fill="white" xmlns="http://www.w3.org/2000/svg">
                        <path fill-rule="evenodd" d="M2 10.5a.5.5 0 01.5-.5h3a.5.5 0 010 1h-3a.5.5 0 01-.5-.5zm0-3a.5.5 0 01.5-.5h7a.5.5 0 010 1h-7a.5.5 0 01-.5-.5zm0-3a.5.5 0 01.5-.5h11a.5.5 0 010 1h-11a.5.5 0 01-.5-.5z" clip-rule="evenodd"/>
                    </svg>
                </a>               
            </button>
            <div class="collapse navbar-collapse" id="navbarNavDropdown" style="justify-content: flex-end;">
                <ul class="navbar-nav">
                    <li class="nav-item"><a href="{%  url 'about' %}" class="nav-link" style="color: white; font-family: 'Cuprum', sans-serif;">About</a></li>
                    <li class="nav-item"><a href="{% url 'contact'%}" class="nav-link" style="color: white; font-family: 'Cuprum', sans-serif;">Contact</a></li>
                    <li class="nav-item"><a href="{% url 'projects' %}" class="nav-link" style="color: white; font-family: 'Cuprum', sans-serif;">Projects</a></li>
                    <li class="nav-item"><a  class="nav-link" style="color: white; font-family: 'Cuprum', sans-serif;" class="btn btn-primary" data-toggle="collapse" href="#collapseExample" role="button" aria-expanded="false" aria-controls="collapseExample">Search</a></li>

                </ul>
            </div>
          </nav>
        </div>
    </header> 
    <!-- Header Ends -->

    <!-- Search -->
    <div class="container-fluid">
        <div class="row justify-content-center">
                <div class="col-lg-9 col-md-12 col-12">
                    <div class="collapse" id="collapseExample">
                        <div class="card card-body" style="background-color: black; border-radius: 0px;">
                            <div class="input-group mb-3">
                                <input type="text" class="form-control" placeholder="Search" aria-label="Search" aria-describedby="button-addon2">
                                <div class="input-group-append">
                                    <button class="btn btn-outline-secondary search-button" type="button" id="button-addon2">
                                        <a href="{% url 'search' %}"><svg class="bi bi-search" width="1em" height="1em" viewBox="0 0 16 16" fill="white" xmlns="http://www.w3.org/2000/svg">
                                            <path fill-rule="evenodd" d="M10.442 10.442a1 1 0 011.415 0l3.85 3.85a1 1 0 01-1.414 1.415l-3.85-3.85a1 1 0 010-1.415z" clip-rule="evenodd"/>
                                             <path fill-rule="evenodd" d="M6.5 12a5.5 5.5 0 100-11 5.5 5.5 0 000 11zM13 6.5a6.5 6.5 0 11-13 0 6.5 6.5 0 0113 0z" clip-rule="evenodd"/>
                                        </svg></a>
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
    </div><!-- Search -->

#this is where I am using for loop for recents

    <!-- Main Starts -->
    <div class="container">

        <div class="row">

                <div class="col-lg-9 col-md-8 col-sm-6 col-12">
                    <div class="card" style="margin-top: 10px;">
                        <div class="row">
                            {% for about in object_list %}
                                {% load static %}

                                <div class="col-lg-4 col-md-6 col-12" >
                                    <a href="{% url 'details' about.id  %}" style="text-decoration: none;">
                                        <div class="card" style="margin-top: 10px; margin-bottom: 10px;">
                                            <img src="{{about.image.url}}" height="260" width="auto">
                                                <div class="card-body" style="text-align: center; background-color: black; padding-top: 10px;padding-bottom: 10px;">
                                                    <h1 class="card-text" style="font-size: 17px; color: white; font-family: 'Rubik', sans-serif;">{{ about.name}}</h1>

                                                </div>
                                        </div>
                                    </a>
                                </div>
                            {% endfor %}
                        </div>
                    </div>
                </div>

            <div class="col-lg-3 col-md-4 col-12">
                <div class="card" style="margin-top: 10px; text-align: center;">
                    <h4 style="margin-top: 20px; text-decoration: underline; font-family: 'Poppins', sans-serif;" >Recent Posts</h4>
                    <div style="text-align: center;">                                       
                        <ul style="list-style: none; padding-left: 0px; margin-top: 10px;">
                            {% for post in recents %}
                                <li>
                                    <a href="#" style="text-decoration: none; color: black; font-family: 'Rubik', sans-serif;">
                                        <h6>
                                            {{ recent.name }}
                                        </h6>
                                    </a>
                                </li>
                            {% endfor %}
                        </ul>                                                       
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- Main Ends -->

{% endblock content %} 

A:

You can limit the results of the query
recents = About.objects.all()[:10]

