开发者

Django problematic view

I have this Django generic view:

def post_list(request, page=0, paginate_by=1, **kwargs):
page_size = getattr(settings,'BLOG_PAGESIZE', paginate_by)
return list_detail.object_list(
    request,
    queryse开发者_运维知识库t=Post.objects.published(),
    paginate_by=page_size,
    page=page,
    **kwargs
)
post_list.__doc__ = list_detail.object_list.__doc__

and I want to send it the current year to use it in the template. Can anyone help me?


Pass the year in via extra_context, for the current year:

from datetime import datetime
year = datetime.now().year


You core problem is using generic views - http://stdout.be/2010/05/25/generic-views-suck/

def post_list(request, page=0, paginate_by=1, **kwargs):
    page_size = getattr(settings,'BLOG_PAGESIZE', paginate_by)
    queryset = Post.objects.published()
    paginator = Paginator(queryset, page_size) 
    objects = paginator.page(page)

    #This stuff is only necessary if you aren't trapping page in the URL conf
    #page = request.GET.get('page')
    #try:
    #    objects = paginator.page(page)
    #except PageNotAnInteger:
    #    # If page is not an integer, deliver first page.
    #    objects = paginator.page(1)
    #except EmptyPage:
    #    # If page is out of range (e.g. 9999), deliver last page of results.
    #    objects = paginator.page(paginator.num_pages)

    return render_to_response('mytemplate.html', {"objects": objects, "year": datetime.now().year}, context=RequestContext(request))

Compared to the original, which code would you rather maintain or extend?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜