[Django]-Django Paginate by Year

7👍

With the new Class Based Views the easies would be to use the YearArchiveView

class ArticleYearArchiveView(YearArchiveView):
    model = Article
    paginate_by = 100
    context_object_name = 'article_list'
    date_field = 'date'
    allow_empty = True

Allow empty is to show the page even when there’s no elements for that year

in your urls.py you would need something like

url(r'^newsitems/(?P<year>\d+)/$', ArticleYearArchiveView.as_view()),

1👍

Yes your model is fine.. Why not write a class based view?

class SomeListView(ListView):
    model = Article
    paginate_by = 100
    template_name = "app/template.html"
    context_object_name = "articles" # or use object_list

    def get_queryset(self):
        return Article.objects.order_by('-date')

You can add a different filter in get_queryset to create a list with results by year, and in your template you can iterate over them and create custom buttons (with year info)

Leave a comment