Reputation: 691
On my blog, I am trying to create a URL pager system where I can link to the previous and next post by the ID of the Post model. Right now, every time I try to go to a blog web page I get this error:
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">←</span> Older</a></li>
<li class="next "><a href="{% url 'blog:blog_detail_url' Next_Post.id %}">Newer <span
aria-hidden="true">→</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">←</span> Older</a></li>
<li class="next"><a href="{% url 'blog:blog_detail_url' Next_Post.id %}">Newer <span aria-hidden="true">→</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">←</span>
Older</a></li>
<li class="next disabled"><a href="">Newer <span aria-hidden="true">→</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">←</span> Older</a></li>
<li class="next disabled"><a href="{% url 'blog:blog_detail_url' Next_Post.id %}">Newer <span aria-hidden="true">→</span></a></li>
</ul>
<h1>4</h1>
{% endif %}
</div>
</center>
</div>
{% endblock %}
The error is happening at line 22 in post.html (<li class="previous"><a href="{% url 'blog:blog_detail_url' Post.id %}"><span aria-hidden="true">←</span> Older</a></li>
). I'm guessing, I imporperly set up the jinja2 href?
Upvotes: 0
Views: 92
Reputation: 2214
Change your links from
"{% url 'blog:blog_detail_url' Next_Post.id %}"
to
"{% url 'blog:blog_detail_url' id=Next_Post.id %}"
Notice the id=
. Your url pattern specifies id
as a url kwarg.
Also, as a rule of thumb, you should not name your variables with upper-case letters. In Python, it's standard to use lower-case variable names, and then have upper case names for classes. When this is done consistently, it helps make your code more readable.
EDIT
This also seems a little fishy to me: {% if Next_Post is defined and Previous_Post is defined %}
what is defined
? I don't see it in the context or anywhere else. I would do this:
{% if next_post %}
{# ... #}
{% endif %}
Upvotes: 1