Reputation: 1
I'm ratherly amatour in django and cant solve this problem,
error: NoReverseMatch at /blog/
Reverse for 'single' with keyword arguments '{'pid': ''}' not found. 1 pattern(s) tried: \['blog/(?P\<pid\>\[0-9\]+)\\Z'\]
urls.py :
from django.urls import path
from blog.views import \*
from django.conf.urls.static import static
app_name= 'blog'
urlpatterns = \[
path('',home,name='home'),
path('\<int:pid\>',single, name='single'),
\]
views.py :
from django.shortcuts import render
from blog.models import Post
import datetime
def single(request,pid):
single_post= Post.objects.filter(pk=pid)
def counting_single_views(n):
n.counted_views += 1
n.save()
counting_single_views(single_post)
context = {'single_post':single_post}
return render(request,'blog/blog-single.html',context)
def home(request):
now = datetime.datetime.now()
posts= Post.objects.filter(published_date__lte= now)
context={'posts':posts}
return render(request,'blog/blog-home.html',context)
blog-home.html :
{% for post in posts %}
\<a href="{% url 'blog:single' pid=post.pk %}"\>\<h3\>{{post.title}}\</h3\>\</a\>
\<p class="excert"\>
{{post.content}}
\</p\>
{% endfor %}
i tried with id instead of pk , but no differ,
Upvotes: 0
Views: 48
Reputation: 1
i got my answer, infact i didn't change any of url tag, i used
{% url 'blog:single' pid=post.pk %}
but in 'base.html' used
{% url 'blog:single' %}
i changed this and NoReverseMatch solved. thanks everyone.
Upvotes: 0
Reputation: 160
In your url file
path('\<int:pid\>',single, name='single'),
Replace it with
path('<int:pid>',single, name='single'),
and also note [#Manoj Kamble] point that can also happen
Upvotes: 0