用基于类的基于日期的通用视图替换 num_latest?

发布于 2024-12-03 19:34:45 字数 802 浏览 0 评论 0原文

我已切换到 Django 1.3,以便为基于日期的通用视图获取分页。这工作正常,但是有一个页面我想要特定数量的项目但不希望它分页。例如,返回前 5 条新闻条目。

在 1.2 中,我们有 num_latest,我们可以将其放入信息字典中以获取最新项目。这在新的基于类的通用视图中似乎不存在。

我可以将 paginate_by 设置为 5 并且不使用模板中的分页链接,但是人们仍然可以通过手动输入 url 来查看旧条目(我不希望这样做)。此外,我不希望 Django 设置我不会使用的分页。

编辑:这是我当前正在使用的 urlconf 行:

url(r'^$', 
    ArchiveIndexView.as_view(
        model = Entry,
        context_object_name = 'entry_list',
        template_name = 'news/news.html',
        date_field = 'published',
    ), name = 'archive_index'
),

进一步编辑:尝试覆盖 get_dated_queryset 我已经将这段代码与上面的 urlconf 结合使用,但使用了名为的新视图:

class MainIndex(ArchiveIndexView):
    def get_dated_queryset(self):
        return Entry.objects.all()[:2]

我得到了与提到的几乎相同的错误在评论中: 一旦获取了切片,就无法对查询重新排序。

I've switched to Django 1.3 in order to get pagination for my date based generic views. This works fine, however there is a page where I want a specific number of items but do not want it paginated. For example, return the first 5 news entries.

In 1.2 we had num_latest which we could put in our info dict to get the latest items. This doesn't seem to exist with the new class-based generic views.

I could set paginate_by to 5 and just not use the pagination links in the template, but then people will still be able to see the old entries by punching in the url manually (which I don't want). Furthermore I don't want Django to set up pagination that I'm not going to use.

Edit: This is the urlconf line I'm currently using:

url(r'^

Further edit: Attempting to override get_dated_queryset I've used this bit of code in conjunction with the urlconf as above but with the new view called:

class MainIndex(ArchiveIndexView):
    def get_dated_queryset(self):
        return Entry.objects.all()[:2]

I get almost the same error as mentioned in the comments:
Cannot reorder a query once a slice has been taken.

, ArchiveIndexView.as_view( model = Entry, context_object_name = 'entry_list', template_name = 'news/news.html', date_field = 'published', ), name = 'archive_index' ),

Further edit: Attempting to override get_dated_queryset I've used this bit of code in conjunction with the urlconf as above but with the new view called:

I get almost the same error as mentioned in the comments:
Cannot reorder a query once a slice has been taken.

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

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

发布评论

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

评论(2

肩上的翅膀 2024-12-10 19:34:45

尝试重写它:
<代码>

def get_dated_items(self):
    date_list, items, extra_context = super(MainIndex, self).get_dated_items()
    return (date_list, items[:2], extra_context)

Note: this implementation may leave the date_list inconsistent with the items query set after the latter is sliced. I think that to fix that you would need to regenerate date_list too. see the implementation of BaseArchiveIndexView.get_dated_items in SVN for more details: http://code.djangoproject.com/browser/django/trunk/django/views/generic/dates.py.
Something like this might work:

def get_dated_items(self):
    date_list, items, extra_context = super(MainIndex, self).get_dated_items()
    items = items[:2]
    date_list = self.get_date_list(items, 'year')
    if not date_list:
        items = items.none()
    return (date_list, items, extra_context)


but if it works without this, I would not touch it because it looks too messy.

Try overriding this instead:

def get_dated_items(self):
    date_list, items, extra_context = super(MainIndex, self).get_dated_items()
    return (date_list, items[:2], extra_context)


Note: this implementation may leave the date_list inconsistent with the items query set after the latter is sliced. I think that to fix that you would need to regenerate date_list too. see the implementation of BaseArchiveIndexView.get_dated_items in SVN for more details: http://code.djangoproject.com/browser/django/trunk/django/views/generic/dates.py.
Something like this might work:

def get_dated_items(self):
    date_list, items, extra_context = super(MainIndex, self).get_dated_items()
    items = items[:2]
    date_list = self.get_date_list(items, 'year')
    if not date_list:
        items = items.none()
    return (date_list, items, extra_context)


but if it works without this, I would not touch it because it looks too messy.

雄赳赳气昂昂 2024-12-10 19:34:45

我自己也遇到了这个问题。我发现使用 ListView(而不是 ArchiveIndexView)可以节省我的时间和麻烦。

对于您的第一块代码,区别在于:

from django.views.generic import ListView


url(r'^
, 
    ListView.as_view(
        model = Entry,
        context_object_name = 'entry_list',
        template_name = 'news/news.html',
        queryset=Entry.objects.all().order_by("-published")[:2],
    ), name = 'archive_index'
),

I ran into this exact problem myself. I've found that using ListView (instead of ArchiveIndexView) for this saved me time and hassle.

For your first chunk of code, the difference would be:

from django.views.generic import ListView


url(r'^
, 
    ListView.as_view(
        model = Entry,
        context_object_name = 'entry_list',
        template_name = 'news/news.html',
        queryset=Entry.objects.all().order_by("-published")[:2],
    ), name = 'archive_index'
),
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文