如何限制 django 视图内的 get_next_by_FOO ?包含代码
我在视图中使用了 get_next_by_FOO 来显示一组记录中下一项的绝对 url,但是当它到达查询集末尾时,会引发错误。
如何阻止我的视图引发此错误,而只是输出一些 html 让我知道它已经到达集合的末尾?
非常感谢所有帮助。
这是错误消息、我的视图和模板代码。
错误
Exception Type: DoesNotExist
Exception Value: Body matching query does not exist.
视图
def news_view(request, url):
news = get_object_or_404(Body, url=url)
next = news.get_next_by_published()
pre = news.get_previous_by_published()
return render_to_response('news/news_view.html', {
'news': news,
'pre': pre,
'next': next
}, context_instance=RequestContext(request))
模板
<a href="{{ next.get_absolute_url }}">Next News</a></p>
<a href="{{ pre.get_absolute_url }}">Previous News</a></p>
I have used get_next_by_FOO inside my view to display an absolute url for the following item within a set of records however when it gets to the end of the query set an error is raised.
how do I stop my view from raising this error and instead just outputting some html letting me know that it has come to the end of the set?
All help is greatly appreciated.
Here is the error message, my view and template code.
Error
Exception Type: DoesNotExist
Exception Value: Body matching query does not exist.
View
def news_view(request, url):
news = get_object_or_404(Body, url=url)
next = news.get_next_by_published()
pre = news.get_previous_by_published()
return render_to_response('news/news_view.html', {
'news': news,
'pre': pre,
'next': next
}, context_instance=RequestContext(request))
Template
<a href="{{ next.get_absolute_url }}">Next News</a></p>
<a href="{{ pre.get_absolute_url }}">Previous News</a></p>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
蛮力答案是:
模板:
The brute force answer is:
Template:
我这样做:
模板:
I do this:
Template: