7👍
✅
Subclass ListView
and override the get_queryset
method. By calculating the startdate
and enddate
inside the get_queryset
method, timezone.now()
will be the time when the request was made, not when the urls.py
was initially loaded.
class TagListView(ListView):
def get_queryset(self):
startdate = timezone.now() - datetime.timedelta(days=7)
enddate = timezone.now()
return Tag.objects.filter(last_view__range=[startdate, enddate]).order_by('-views')[:10]
context_object_name='most_viewed_list'
template_name='tags/index.html'
urlpatterns = patterns('',
url(r'^$', TagListView.as_view(), name='index'),
)
Source:stackexchange.com