Django search view works, but clicking results gives “No Blog matches the given query” 404 error

Question Body

I have implemented a search functionality in my Django blog app. The search form works and displays results, but when I click on a blog from the search results, I get the following error:

Page not found (404)
No Blog matches the given query.

Here is my code:


base.html

<div class="col-4">
    <form action="{% url 'search' %}" method="GET">
        <div class="input-group">
            <input class="form-control" type="text" name="keyword" placeholder="Enter search term..." />
            <button class="btn btn-warning" type="submit">Go!</button>
        </div>
    </form>
</div>


urls.py

from blogs import views as BlogView
from django.urls import path

urlpatterns = [
    path('blogs/<slug:slug>/', BlogView.blogs, name='blogs'),
    path('blogs/search/', BlogView.search, name='search'),
]


views.py

from django.db.models import Q
from django.shortcuts import render
from .models import Blog

def search(request):
    keyword = request.GET.get('keyword')
    blogs = Blog.objects.filter(
        Q(title__icontains=keyword) |
        Q(short_description__icontains=keyword) |
        Q(blog_body__icontains=keyword),
        status='Published'
    )
    return render(request, 'search.html', {'blogs': blogs, 'keyword': keyword})


search.html

{% extends 'base.html' %}

{% block content %}
<h3 class="text-warning" style="letter-spacing:2px;">Search-Term-{{ keyword }}</h3>

<div class="row mb-2">
    {% if blogs %}
        {% for i in blogs %}
            <div class="col-md-6">
                <div class="card border-0">
                    <div class="card-body">
                        <h3><a href="{% url 'blogs' i.slug %}">{{ i.title }}</a></h3>
                        <small>{{ i.created_at|timesince }} ago | {{ i.author }}</small>
                        <p>{{ i.short_description|truncatewords:25 }}</p>
                    </div>
                </div>
            </div>
        {% endfor %}
    {% else %}
        <p>No Post Found</p>
    {% endif %}
</div>
{% endblock %}

Side note: You need to enclose the block of code between lines of three backticks - ```, not tildes (~). Also, those backticks must be lines by themselves and not part of the lines of code. (I’ve corrected your original post for this.)

Please provide more detail about which page you were on before getting this message, and what the url is that was being requested.

Also please post the complete urls.py file for the project and if this is an app’s url file you are showing here, the complete file for the app.