[Django]-Adding a maximum limit to the number of post using python

5πŸ‘

βœ…

I think you have another template for displaying categorised objects when you click category button. As you said

"I should only have the 15 most recent posts in my blog. The rest can
be seen by clicking on the category button."

In this case you can use a simple hack to display most recent posts from your table.
query all objects in descending order in views

    all_objs = Post.objects.all().order_by('-id')

Then use {% if forloop.counter <= 15 %} to display last 15 items only. as follow.

templates

{% for post in object_list %}

   {% if forloop.counter <= 15 %}

       <h4>{{obj}} #or anything really that is meant to be shown on the home page.</h4>

   {% endif %}

{% endfor %}

1πŸ‘

You can do something like this:

def get_context_data(self, *args, **kwargs):
    context = super(HomeView, self).get_context_data(*args,**kwargs)
    context["cat_menu"] = Category.objects.all()
    context["most_recent_posts"] = Post.objects.filter(author=self.request.user).order_by('-post_date')[:15]
    return context  

This will get the 15 most recent posts authored by the current user, ordered by the date it was posted.

Then just handle displaying this in home.html for example:

<ul>
{% for p in most_recent_posts %}
    <li>{{ p.title }}</li>
{% endfor %}
</ul>
πŸ‘€Brian Destura

1πŸ‘

Just limit your query to the latest 15 entries sorted by post_date:

cat_menu = Category.objects.latest("post_date")[:15]

1πŸ‘

https://docs.djangoproject.com/en/3.2/topics/pagination/

The best way is Django Pagintion.

{% for contact in page_obj %}
    {# Each "contact" is a Contact model object. #}
    {{ contact.full_name|upper }}<br>
    ...
{% endfor %}

<div class="pagination">
    <span class="step-links">
        {% if page_obj.has_previous %}
            <a href="?page=1">&laquo; first</a>
            <a href="?page={{ page_obj.previous_page_number }}">previous</a>
        {% endif %}

        <span class="current">
            Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
        </span>

        {% if page_obj.has_next %}
            <a href="?page={{ page_obj.next_page_number }}">next</a>
            <a href="?page={{ page_obj.paginator.num_pages }}">last &raquo;</a>
        {% endif %}
    </span>
</div>
from django.core.paginator import Paginator
from django.shortcuts import render

from myapp.models import Contact

def listing(request):
    contact_list = Contact.objects.all()
    paginator = Paginator(contact_list, 25) # Show 25 contacts per page.

    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)
    return render(request, 'list.html', {'page_obj': page_obj})
πŸ‘€xxxecute

0πŸ‘

you can use Django pagination api . Manage your data through page number. Initially pass 1 and after that page number given by pagination.

 paginator = Paginator(yourquerysetdata, 20)
        page_num = request.data.get('page')
        result = yourSerializerName(paginator.get_page(page_num) many=True).data
        try:
            page = paginator.page(page_num)
        except:
            page = paginator.page(1)

        count = paginator.num_pages

        resultobj = paginator.get_page(page_num)
        has_prev = resultobj.has_previous()
        has_next = resultobj.has_next()
        page_range = resultobj.paginator.page_range.stop - 1
        if has_prev:
            prev_page_no = resultobj.previous_page_number()
        else:
            prev_page_no = 0

        if has_next:
            next_page_no = resultobj.next_page_number()
        else:
            next_page_no = None

        context = dict()
        context['page'] = page.number
        context['page_no'] = count
πŸ‘€Pradeep Kumar

0πŸ‘

It is very simple. You just have to modify the query that you are using to fetch the posts.
In the get_context_data() method, replace cat_menu = Category.objects.all()
with cat_menu = Category.objects.all().order_by('-post_date')[:15]. This will limit the number of results to 15 most recent objects.
For more understanding, you can take a look at the official Django docs for Limiting QuerySets.

Leave a comment