简体   繁体   中英

can solve this error ? ( NoReverseMatch )

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,

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM