Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a maximum limit to the number of post using python

Tags:

python

django

I need to limit the number of posts in Django queries. I have tried to add a min and max but nothing seemed to have worked. I have added home.html into the code.

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

Home.html:

{% extends 'base.html' %}

{% block content %}




<h1>Posts</h1>

<ul>
{% for post in object_list %}

    <li><a href="{% url 'article-detail' post.pk %}">{{post.title}}</a> 
<style>
    a {
        text-transform: capitalize;
    }
</style>
        - <a href="{% url 'category' post.category %}">{{ post.category }}</a> - <a href="{% url 'show_profile_page' post.author.profile.id %}">{{ post.author.first_name }} 
    {{ post.author.last_name }}</a> - {{ post.post_date }} <small>

        {% if user.is_authenticated %}
            {% if user.id == post.author.id %}

            - <a href="{% url 'update_post' post.pk %}">(Edit)</a>

            <a href="{% url 'delete_post' post.pk %}">(Delete)</a>
            {% elif user.id == 1 %}

            - <a href="{% url 'update_post' post.pk %}">(Edit)</a>

            <a href="{% url 'delete_post' post.pk %}">(Delete)</a>

            {% endif %}

        {% endif %}


    </small><br/>
    {{ post.snippet }}</li>
{% endfor %}
</ul>


{% endblock %}

view.py:

class HomeView(ListView):
    model = Post
    template_name = 'home.html'
    ordering = ['-id']

    def get_context_data(self, *args, **kwargs):
            cat_menu = Category.objects.all()
            context = super(HomeView, self).get_context_data(*args,**kwargs)
            context["cat_menu"] = cat_menu
            return context

models.py:

class Post(models.Model):
    title = models.CharField(max_length=255)
    header_image = models.ImageField(null=True, blank=True, upload_to='images/')
    title_tag = models.CharField(max_length=255)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    body = RichTextField(blank=True, null=True)
    post_date = models.DateField(auto_now_add=True)
    category = models.CharField(max_length=255, default='intro')
    snippet = models.CharField(max_length=255)
    likes = models.ManyToManyField(User, related_name='post_likes')
    dislikes = models.ManyToManyField(User, related_name='post_dislikes')
like image 304
Kaushik Avatar asked Apr 17 '26 04:04

Kaushik


1 Answers

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 %}
like image 105
Rasheed kotoor Avatar answered Apr 20 '26 02:04

Rasheed kotoor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!