I've been practicing Djnago and for that, I was building a blog. While in the process of building it, I was faced with an error, while using the following code:
<a href="{% url 'blog_post' post.slug %}">
{{ post.title }}
</a>
While studying and doing other courses something like this would work fine. But now, it will raise this exception: NoReverseMatch.
If I use this code though, it will work just fine:
<a href="{{ post.slug }}">
{{ post.title }}
</a>
While working in different projects the first way would work fine, but this time, it doesn't.
My question is why?
Here is the code in my urls and on my views. Maybe the mistake is here, not somewhere else.
If anyone can explain why this is happening, or where I'm going wrong, it will be greatly appreciated
urls:
from django.urls import path
from . import views
app_name = 'blog'
urlpatterns = [
path('', views.blog_index, name='blog_index'),
path('<slug:post_slug>/', views.blog_post, name='blog_post'),
]
views:
from django.shortcuts import render
from .models import Post
# Create your views here.
def blog_index(request):
posts = Post.objects.order_by('-
created').filter(published=True)
data = {
'posts': posts
}
return render(request, 'blog/post_list.html', data)
def blog_post(request, post_slug):
post = Post.objects.get(slug=post_slug)
data = {
'post': post
}
return render(request, 'blog/post_detail.html', data)
NoReverseMatch
is not sufficient to debug the issue.post
) that caused the issue for starters. Also an MCVE is not an excuse for missing debugging information (such as the error message).