X
X
XisterONE2017-06-24 20:17:19
Django
XisterONE, 2017-06-24 20:17:19

How to paginate Django pages?

Here is views.py

def TaskList(request, category_slug=None):
    category = None
    categories = Category.objects.all()
    tasks = Task.objects.all()
    if category_slug:
        category = get_object_or_404(Category, slug=category_slug)
        tasks = tasks.filter(category=category)
    return render(request, 'list.html', locals())

Here is the html
{% extends 'my_bootstrap3.html' %}
{% load bootstrap3 %}
{% block title %}
  {% if category %} {{ category.name }} {% else %} Сторінка завдань {% endif %}
{% endblock %}
{% block bootstrap3_content %}
    <div class="container">
        <div class="row">
            <button type="button" class="btn btn-default btn-circle btn-lg pull-right"><a href="/tasks/new/"><i class="glyphicon glyphicon-plus"></i></a></button>
        </div>
        <div class="row">
            <div id="sidebar" class="col-sm-3">
                <h3>Категории</h3>
                <ul>
                    <li {% if not category %} class="selected" {% endif %}>
                        <button class="btn btn-default filter-button" ><a href="/tasks/">Все</a></button>
                    </li>
                    {% for cat in categories %}
                    <li {% if category.slug == cat.slug %} class="selected" {% endif %}>
                        <button class="btn btn-default filter-button" ><a href="{{ cat.get_absolute_url }}">{{ cat.name }}</a></button>
                    </li>
                    {% endfor %}
                </ul>
            </div>
            <div id="main" class="task_list col-sm-9">
                <h1>{% if category %}{{ category.name }}{% else %}Завдання{% endif %}</h1>
                 {% for task in tasks %}
                <article>
                    <div class="row">
                        <div class="col-sm-6 col-md-4">
                            <figure>
                                <img src="http://usbcode.ir/wp-content/uploads/2017/03/bootstrap.jpg">
                            </figure>
                        </div>
                        <div class="item col-sm-8">
                            <h4>{{ task.name }}</h4>
                            <p>{{ task.description }}</p>
                            <section>
                                <i class="glyphicon glyphicon-user"></i>{{ task.user }}
                                <i class="glyphicon glyphicon-calendar"></i>{{ task.created }}
                                <a href="{{ task.get_absolute_url }}" class="btn btn-default btn-sm pull-right">Деталі</a>
                            </section>
                        </div>
                    <br><br>
                    </div>
            </article>
                {% endfor %}
            </div>
        </div>
        <div class="row">
            <div class="col-lg-12" align="center">
                <div class="pagination">
                    {% if tasks.has_previous %}
                        <a href="?page={{ tasks.previous_page_number }}"></a>
                    {% endif %}

                    {% for num in tasks.paginator.page_range %}
                        {% if num == tasks.number %}
                            <span class="current"><b>{{ num }}</b></span>
                        {% else %}
                            <a href="?page={{ num }}"> {{ num }}</a>
                        {% endif %}
                    {% endfor %}

                    {% if article_list.has_next %}
                        <a href="?page={{ article_list.next_page_number }}"></a>
                    {% endif %}
            </div>
        </div>
    </div>
    </div>
{% endblock%}

Here is models.py
class Category(models.Model):
    name = models.CharField(max_length=200, db_index=True, verbose_name='Имя')
    slug = models.SlugField(max_length=200, db_index=True, unique=True)

    class Meta:
        ordering = ['name']
        verbose_name = 'Категория'
        verbose_name_plural = 'Категории'

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('task:TaskListByCategory', args=[self.slug])


# Модель продукта
class Task(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1, verbose_name="Автор")
    category = models.ForeignKey(Category, related_name='tasks', verbose_name="Категория")
    name = models.CharField(max_length=200, db_index=True, verbose_name="Название")
    slug = models.SlugField(max_length=200, db_index=True)
    description = models.TextField(blank=True, verbose_name="Описание")
    created = models.DateTimeField(auto_now_add=True, verbose_name='Создан')
    updated = models.DateTimeField(auto_now=True, verbose_name='Обновлен')

    class Meta:
        ordering = ['name']
        index_together = [
            ['id', 'slug']
        ]
        verbose_name = 'Задание'
        verbose_name_plural = 'Задания'

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('task:TaskDetail', args=[self.id, self.slug])

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2017-06-28
@bergsoft

Try the application that we support
https://github.com/shtalinberg/django-el-pagination
where you can choose the desired pagination style.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question