用基于类的基于日期的通用视图替换 num_latest?
我已切换到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试重写它:
<代码>
Note: this implementation may leave the
date_list
inconsistent with theitems
query set after the latter is sliced. I think that to fix that you would need to regeneratedate_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:
but if it works without this, I would not touch it because it looks too messy.
Try overriding this instead:
Note: this implementation may leave the
date_list
inconsistent with theitems
query set after the latter is sliced. I think that to fix that you would need to regeneratedate_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:
but if it works without this, I would not touch it because it looks too messy.
我自己也遇到了这个问题。我发现使用 ListView(而不是 ArchiveIndexView)可以节省我的时间和麻烦。
对于您的第一块代码,区别在于:
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: