Django错误:反向'blog_detail_url',参数'('',)'和关键字参数'{}'未找到。尝试了1种模式:['blog /(?P <id> \\ d +)$']

时间:2017-01-22 06:51:30

标签: python django

在我的博客上,我正在尝试创建一个URL寻呼机系统,我可以通过Post模型的ID链接到上一篇和下一篇文章。现在,每当我尝试访问博客网页时,我都会收到此错误:

NoReverseMatch at /blog/2
Reverse for 'blog_detail_url' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['blog/(?P<id>\\d+)$']

views.py:

def blog_detail(request, id):
    # post request from the url
    return render(request, "BlogHome/pages/post.html")
    post = Post.objects.get(id)

    # Next Post
    try:
        Next_Post_id = (post.id + 1)
        Next_Post = Post.objects.get(id=Next_Post_id)
    except ObjectDoesNotExist:
        Next_Post = None

    # Previous Post
    try:
        Previous_Post_id = (post.id - 1)
        Previous_Post = Post.objects.get(id=Previous_Post_id)
    except ObjectDoesNotExist:
        Previous_Post = None

    context = {'post': post, 'Next_Post': Next_Post, 'Previous_Post': Previous_Post}
    return render(request, "BlogHome/pages/post.html", context)

urls.py:

urlpatterns = [
url(r'^$', views.blog_list),
url(r'^(?P<id>\d+)$', views.blog_detail, name='blog_detail_url'),
]

post.html:

{% extends "BlogHome/includes/WELL.html" %}

{% block content %}
<script>
document.title = "Pike Dzurny | {{post.title}}"
</script>

<div class="container-fluid text-center">

    <center>

        <div class="well" id="WellPost">

            <div class="container-fluid">

                <h2 align="center" id="TitleText">{{post.title}}</h2>
                <h3 align="center" id="BodyText">{{ post.date|date:"m-d"}}</h3>
                <h3 align="left">{{ post.body|safe }}</h3>

                {% if Next_Post is defined and Previous_Post is defined %}
                <ul class="pager">
                    <li class="previous"><a href="{% url 'blog:blog_detail_url' Post.id %}"><span
                            aria-hidden="true">&larr;</span> Older</a></li>
                    <li class="next "><a href="{% url 'blog:blog_detail_url' Next_Post.id %}">Newer <span
                            aria-hidden="true">&rarr;</span></a></li>
                    <h1>1</h1>
                </ul>

                {% elif Next_Post is defined %}
                <ul class="pager">
                    <li class="previous disabled"><a href=""><span aria-hidden="true">&larr;</span> Older</a></li>
                    <li class="next"><a href="{% url 'blog:blog_detail_url' Next_Post.id %}">Newer <span aria-hidden="true">&rarr;</span></a>
                    </li>
                </ul>
                <h1>2</h1>

                {% elif Previous_Post is defined %}
                <ul class="pager">
                    <li class="previous"><a href="{% url 'blog:blog_detail_url' Post.id %}"><span aria-hidden="true">&larr;</span>
                        Older</a></li>
                    <li class="next disabled"><a href="">Newer <span aria-hidden="true">&rarr;</span></a></li>
                </ul>
                <h1>3</h1>

                {% else %}
                <ul class="pager">
                    <li class="previous disabled"><a href="{% url 'blog:blog_detail_url' Post.id %}"><span aria-hidden="true">&larr;</span> Older</a></li>
                    <li class="next disabled"><a href="{% url 'blog:blog_detail_url' Next_Post.id %}">Newer <span aria-hidden="true">&rarr;</span></a></li>
                </ul>
                <h1>4</h1>
                {% endif %}


            </div>
    </center>
</div>

{% endblock %}

错误发生在post.html(<li class="previous"><a href="{% url 'blog:blog_detail_url' Post.id %}"><span aria-hidden="true">&larr;</span> Older</a></li>)的第22行。我猜,我不情愿地设置了jinja2 href?

1 个答案:

答案 0 :(得分:1)

更改您的链接
"{% url 'blog:blog_detail_url' Next_Post.id %}"

"{% url 'blog:blog_detail_url' id=Next_Post.id %}"

注意id=。您的网址格式指定id为网址kourg。

另外,根据经验,您不应使用大写字母命名变量。在Python中,使用小写变量名称是标准的,然后为类使用大写名称。如果一直这样做,它有助于使您的代码更具可读性。

修改

这对我来说似乎有点可疑:{% if Next_Post is defined and Previous_Post is defined %}什么是defined?我不会在上下文或其他任何地方看到它。我会这样做:

{% if next_post %}
    {# ... #}
{% endif %}
相关问题