Django 有问题的视图

发布于 2024-11-29 04:47:56 字数 397 浏览 0 评论 0原文

我有这个 Django 通用视图:

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

并且我想将其发送到当前年份以在模板中使用它。谁能帮助我吗?

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,
    queryset=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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

给我一枪 2024-12-06 04:47:56

通过 < 传递年份code>extra_context,对于当年:

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

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

from datetime import datetime
year = datetime.now().year
久而酒知 2024-12-06 04:47:56

您的核心问题是使用通用视图 - 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))

与原始代码相比,您更愿意维护或扩展哪些代码?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文