Search code examples
pythondjangopaginationblogsarchive

django blog archive, index and pagination comprehensive application


Background:

I am building a blog about my research group. On the publication archive page I am going to display all the publications of my mentor. Here there is a side column to show the archive index allowing users to view the publications by year. And at the bottom of the page there is a django paginator which separate the publications in to several pages with 7 publications per page.

Problem:

When the pagination is used, the publications is divided into a list, so the {{publication.published_time}} only contain the data in the current page rather than the whole dataset. Thus, I wrote the hard code of year information in the front end and add the url to the corresponding year. Apparently, I wish I can get the distinct year information about all publications on the basis of the existence of a paginator. Besides, transfer the year value directly in the URL.

Code:

url.py:

url(r'^publications/(?P<year>[0-9]{4})/$', PublicationYearArchiveView.as_view(),
    name="publication_year_archive"),

views.py:

class PublicationYearArchiveView(YearArchiveView):
    queryset = Publication.objects.all()
    date_field = "published_time"
    make_object_list = True
    allow_future = True

    def listing(request):
        limit = 7
        publication_list = Publication.objects.all().order_by('published_time').reverse()
        paginator = Paginator(publication_list, limit)  # Show 7 publications per page

        page = request.GET.get('page')
        try:
            publications = paginator.page(page)
        except PageNotAnInteger:
            # If page is not an integer, deliver first page.
            publications = paginator.page(1)
        except EmptyPage:
            # If page is out of range (e.g. 9999), deliver last page of results.
            publications = paginator.page(paginator.num_pages)

        return render(request, 
                      'research_spacecoupe/research_accomplishment.html', 
                      {'publications': publications})

research.html:

Paginator:

{% if publications.has_other_pages %}
    <ul class="pagination">
    {% if publications.has_previous %}
        <li><a href="?page={{ publications.previous_page_number }}">&laquo;</a></li>
    {% else %}
        <li class="disabled"><span>&laquo;</span></li>
    {% endif %}

    {% for i in publications.paginator.page_range %}
        {% if publications.number == i %}
        <li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
        {% else %}
        <li><a href="?page={{ i }}">{{ i }}</a></li>
        {% endif %}
    {% endfor %}

    {% if publications.has_next %}
        <li><a href="?page={{ publications.next_page_number }}">&raquo;</a></li>
    {% else %}
        <li class="disabled"><span>&raquo;</span></li>
    {% endif %}
    </ul>
{% endif %}

Index:

<div class="sidebar-module">
    <h4>Archive</h4>
    <ol class="list-unstyled">
        <li><a href="{% url 'research_spacecoupe:listing'  %}">all publications</a></li>
        <li><a href="{% url 'research_spacecoupe:publication_year_archive'   2017 %}">2017</a></li>
        <li><a href="{% url 'research_spacecoupe:publication_year_archive'   2016 %}">2016</a></li>
        <li><a href="{% url 'research_spacecoupe:publication_year_archive'   2015 %}">2015</a></li>
        <li><a href="{% url 'research_spacecoupe:publication_year_archive'   2013 %}">2013</a></li>
        <li><a href="{% url 'research_spacecoupe:publication_year_archive'   2012 %}">2012</a></li>
        <li><a href="{% url 'research_spacecoupe:publication_year_archive'   2011 %}">2011</a></li>
        <li><a href="{% url 'research_spacecoupe:publication_year_archive'   2010 %}">2010</a></li>
    </ol>
</div>

Note:

Now the page does work, and I can click the year(eg. 2017) to view the publications published in 2017 and click the "All publications" to view all publications. But I want to replace the number of year with distinct publications.published_time of all publications. Anything I can do to fix it?


Solution

  • views.py:

    from django.db.models.functions import ExtractYear
    from django.db.models import Count
    
    # ...
    
    def listing(request):
        # ...
    
        years = Publication.objects \
            .annotate(year=ExtractYear('published_time')) \
            .values_list('year') \
            .annotate(count=Count('id')) \
            .values_list('year', flat=True) \
            .order_by('year')
    
        return render(request, 
                      'research_spacecoupe/research_accomplishment.html', 
                      {'publications': publications, 
                       'years': years})
    

    research_accomplishment.html:

    <div class="sidebar-module">
        <h4>Archive</h4>
        <ol class="list-unstyled">
            <li><a href="{% url 'research_spacecoupe:listing' %}">all publications</a></li>
            {% for year in years %}
                <li><a href="{% url 'research_spacecoupe:publication_year_archive' year %}">{{ year }}</a></li>
            {% endfor %}
        </ol>
    </div>