views:

1082

answers:

5

I have a pretty simple question. I want to make some date-based generic views on a Django site, but I also want to paginate them. According to the documentation the object_list view has page and paginate_by arguments, but the archive_month view does not. What's the "right" way to do it?

+1  A: 

Date based generic views don't have pagination. It seems you can't add pagination via wrapping them as well since they return rendered result.

I would simply write my own view in this case. You can check out generic views' code as well, but most of it will probably be unneeded in your case.

Since your question is a valid one, and looking at the code; I wonder why they didn't decouple queryset generation as separate functions. You could just use them and render as you wish then.

muhuk
Yeah, that's exactly what I'm looking for, functions to generate the queryset basted on date parameters. If I have that, then I can just use the object_list or the pagination object. As it is now, I think I'll have to write my own views.
Apreche
I've been frustrated by django's generic views myself, And have basically written a generic-view-factory type god-method so that I don't have the limitations mentioned here.
TokenMacGuy
+3  A: 

I created a template tag to do template-based pagination on collections passed to the templates that aren't already paginated. Copy the following code to an app/templatetags/pagify.py file.

from django.template import Library, Node, Variable
from django.core.paginator import Paginator
import settings

register = Library()

class PagifyNode(Node):
    def __init__(self, items, page_size, varname):
     self.items = Variable(items)
     self.page_size = int(page_size)
     self.varname = varname

    def render(self, context):
     pages = Paginator(self.items.resolve(context), self.page_size)
     request = context['request']
     page_num = int(request.GET.get('page', 1))

     context[self.varname] = pages.page(page_num)
     return ''

@register.tag
def pagify(parser, token):
    """
    Usage:

    {% pagify items by page_size as varname %}
    """

    bits = token.contents.split()
    if len(bits) != 6:
     raise TemplateSyntaxError, 'pagify tag takes exactly 5 arguments'
    if bits[2] != 'by':
     raise TemplateSyntaxError, 'second argument to pagify tag must be "by"'
    if bits[4] != 'as':
     raise TemplateSyntaxError, 'fourth argument to pagify tag must be "as"'
    return PagifyNode(bits[1], bits[3], bits[5])

To use it in the templates (assume we've passed in an un-paginated list called items):

{% load pagify %}

{% pagify items by 20 as page %}
{% for item in page %}
    {{ item }}
{% endfor %}

The page_size argument (the 20) can be a variable as well. The tag automatically detects page=5 variables in the querystring. And if you ever need to get at the paginator that belong to the page (for a page count, for example), you can simply call:

{{ page.paginator.num_pages }}
Soviut
+1  A: 

I was working on a problem similar to this yesterday, and I found the best solution for me personally was to use the object_list generic view for all date-based pages, but pass a filtered queryset, as follows:

import datetime, time

def post_archive_month(request, year, month, page=0, template_name='post_archive_month.html', **kwargs):
    # Convert date to numeric format
    date = datetime.date(*time.strptime('%s-%s' % (year, month), '%Y-%b')[:3])
    return list_detail.object_list(
      request,
      queryset = Post.objects.filter(publish__year=date.year, publish__date.month).order_by('-publish',),
      paginate_by = 5,
      page = page,
      template_name = template_name,
      **kwargs)

Where the urls.py reads something like:

url(r'^blog/(?P<year>\d{4})/(?P<month>\w{3})/$',
    view=path.to.generic_view,
    name='archive_month'),

I found this the easiest way around the problem without resorting to hacking the other generic views or writing a custom view.

Gary Chambers
+1 although it would require you add additional information to to the extra_context to compensate for the additional variables (year, month, etc) that the date-based views pass.
Soviut
A: 

Django date-based generic views do not support pagination. There is an open ticket from 2006 on this. If you want, you can try out the code patches supplied to implement this feature. I am not sure why the patches have not been applied to the codebase yet.

hughdbrown
+1  A: 

There is also excellent django-pagination add-on, which is completely independent of underlying view.

Tomasz Zielinski