如何限制 django 视图内的 get_next_by_FOO ?包含代码

发布于 2024-09-29 07:31:21 字数 823 浏览 4 评论 0原文

我在视图中使用了 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 技术交流群。

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

发布评论

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

评论(2

嘿看小鸭子会跑 2024-10-06 07:31:21

蛮力答案是:

def news_view(request, url):
    news = get_object_or_404(Body, url=url)
    try:
        next = news.get_next_by_published()
    except Body.DoesNotExist:
        next = None
    try:
        pre = news.get_previous_by_published()
    except Body.DoesNotExist:
        pre = None
    return render_to_response('news/news_view.html', {
        'news': news,
        'pre': pre,
        'next': next
        }, context_instance=RequestContext(request))

模板:

{% if next %}<a href="{{ next.get_absolute_url }}">Next News</a></p>{% else %}<span class="disabled">At Last Item</span>{% endif %}
{% if pre %}<a href="{{ pre.get_absolute_url }}">Previous News</a></p>{% else %}<span class="disabled">At First Item</span>{% endif %}

The brute force answer is:

def news_view(request, url):
    news = get_object_or_404(Body, url=url)
    try:
        next = news.get_next_by_published()
    except Body.DoesNotExist:
        next = None
    try:
        pre = news.get_previous_by_published()
    except Body.DoesNotExist:
        pre = None
    return render_to_response('news/news_view.html', {
        'news': news,
        'pre': pre,
        'next': next
        }, context_instance=RequestContext(request))

Template:

{% if next %}<a href="{{ next.get_absolute_url }}">Next News</a></p>{% else %}<span class="disabled">At Last Item</span>{% endif %}
{% if pre %}<a href="{{ pre.get_absolute_url }}">Previous News</a></p>{% else %}<span class="disabled">At First Item</span>{% endif %}
柠檬色的秋千 2024-10-06 07:31:21

我这样做:

def news_view(request, url):
    news = get_object_or_404(Body, url=url)
    return direct_to_template(request, 'news/news_view.html', {'news': news,})

模板:

{% with news.get_previous_by_pub_date as prev %}
   {% if prev %}
       <a class="left btn" href="{{ prev.get_absolute_url }}">Previous News</a>
   {% endif %}
{% endwith %}
{% with news.get_next_by_pub_date as next %}
    {% if next %}
        <a class="right btn" href="{{ next.get_absolute_url }}">Next News</a>
    {% endif %}
{% endwith %}

I do this:

def news_view(request, url):
    news = get_object_or_404(Body, url=url)
    return direct_to_template(request, 'news/news_view.html', {'news': news,})

Template:

{% with news.get_previous_by_pub_date as prev %}
   {% if prev %}
       <a class="left btn" href="{{ prev.get_absolute_url }}">Previous News</a>
   {% endif %}
{% endwith %}
{% with news.get_next_by_pub_date as next %}
    {% if next %}
        <a class="right btn" href="{{ next.get_absolute_url }}">Next News</a>
    {% endif %}
{% endwith %}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文