Post matching query does not exist

Viewed 82

urls.py

path('posts/',views.posts,name='posts'),

path('<id>',views.detail_view,name='detail_view'),

views.py

def posts(request):
    posts = Post.objects.all().order_by('-date_added')
    context = {'posts':posts}
    return render(request, 'mains/post.html', context)

post.html

{% block content %}

{% for topic in posts %}


    <a>{{ topic.description}}</a> <a href="{% url 'mains:detail_view' 
topic.post_owner.id %}">Show 
More</a><br>
    <br>

{% empty %}
  No yet.</li>
{% endfor %}

Post matching query does not exist. This error is raising when i click on show_more in posts page. Please Help me in this. I will really appreciate your Help.

2 Answers

You are creating the hyperlink with post owner's PK, but, your view expecting the Post's PK.

So, update the hyperlink logic in the template as,

<a href="{% url 'mains:detail_view' topic.post_owner.id %}">

to

<a href="{% url 'mains:detail_view' id=topic.id %}">

You should use this

<a href="{% url 'mains:detail_view' topic.id %}">

instead of

<a href="{% url 'mains:detail_view' topic.post_owner.id %}">

Because you're passing pk in views.py but not in your templates

Related