Skip to content

Instantly share code, notes, and snippets.

@dfalk
Created May 4, 2011 08:24
Show Gist options
  • Save dfalk/954924 to your computer and use it in GitHub Desktop.
Save dfalk/954924 to your computer and use it in GitHub Desktop.
django <1.2 date_based object_list
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment