TemplateDoesNotExist at /search/

Hii I’m new to django and i’m creating a search application. When I serach a word this error pops up. Im stuck here for a while can anyone help?

TemplateDoesNotExist at /search/

search_results, home/post_list.html

Request Method: GET
Request URL: http://127.0.0.1:8000/search/?q=thalam
Django Version: 4.0.6
Exception Type: TemplateDoesNotExist
Exception Value: search_results, home/post_list.html
Exception Location: /home/manu/Desktop/django /serv/.venv/lib/python3.10/site-packages/django/template/loader.py, line 47, in select_template
Python Executable: /home/manu/Desktop/django /serv/.venv/bin/python3
Python Version: 3.10.4
Python Path: [‘/home/manu/Desktop/django /serv’, ‘/usr/lib/python310.zip’, ‘/usr/lib/python3.10’, ‘/usr/lib/python3.10/lib-dynload’, ‘/home/manu/Desktop/django /serv/.venv/lib/python3.10/site-packages’]

Hi mdv77,

The error is indicating that the template being used for this view does not exist. You should check the view that handles the /search/ route and see what it tries to use for a template when rendering a response. I suspect it’s "search_results, home/post_list.html". Then replace that value with the path to the correct template you’d like to use.

path(“search_results”, views.search_results, name=“search_results”),
This is the path I used in urls.py

ef search_results(request):

if request.method == "POST":
    searched = request.POST["searched"]
    venue = Post.objects.filter(buisiness_name__contains=searched)
    return render(
        request, "home/search_results.html", {"searched": searched, "venue": venue}
    )
else:
    return render(request, "home/search_results.html", {})

This is the function in view.

But I never created a post_list.html, I dont know where it comes from. But I named my class as Post.

Please post the complete view.

My app is called home

from django.shortcuts import render
from django.views.generic import ListView, DetailView
from django.views.generic.edit import CreateView
from .models import Post


class HomeListView(ListView):
    model = Post
    template_name = "home.html"


class HomeDetailView(DetailView):
    model = Post
    template_name = "post_detail.html"


class HomeCreateView(CreateView):
    model = Post
    template_name = "post_new.html"
    fields = [
        "buisiness_name",
        "city",
        "email",
        "telephone_number",
        "mobile_number",
        "whatsapp_number",
        "description",
    ]


def search_results(request):

    if request.method == "POST":
        searched = request.POST["searched"]
        venue = Post.objects.filter(buisiness_name__contains=searched)
        return render(
            request, "home/search_results.html", {"searched": searched, "venue": venue}
        )
    else:
        return render(request, "home/search_results.html", {})

When posting code here, you need to enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted which is critical with Python. Please edit your post to put ``` on a line above your code and ``` on a line after your code.

Sorry first time here. I edit my comment

What do your URLs look like for these?

from django.urls import path
from . import views
from .views import HomeListView, HomeDetailView, HomeCreateView

urlpatterns = [
    path("post/new/", HomeCreateView.as_view(), name="post_new"),
    path("", HomeListView.as_view(), name="home"),
    path("post/<int:pk>/", HomeDetailView.as_view(), name="post_detail"),
    path("search_results", views.search_results, name="search_results"),
]

Your error message posted here shows you’re trying to access a url /search/. But I don’t see a definition for that in this list.

What view is being called for this url?

When I search keyword “thalam” this is the url.

/search/ is now changed into ‘’'/search_results/```

But you’re showing that you’re trying to access a URL /search/

What is it entry in your urls.py file that will handle that url.


Sorry My net speed is slow. That screenshot was before. Now it changed into this.

What directory is your search_results.html located in?

What is your TEMPLATES setting in your settings.py file.

New Update. When I changed

"home/search_results.html"

into

search_results.html

I got the output.
Screenshot from 2022-07-20 22-21-43
But my keyword is ‘thalam’ and result should be “Thalam food products”. Where does this query set comes from.

code of search_results.html

{% extends "base.html" %}
{% block content %}
<h1>Search Results</h1>
{% if searched %}
<h2>{{ searched }}</h2>
{{ venue }}
{% else %}
<h2>No Results Found</h2>
{% endif %}

{% endblock content %}

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [BASE_DIR / "templates"],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
            ],
        },
    },
]

This creates a queryset - there can be more than one result matching that criteria.

Therefore, you may need to iterate over your set of results in your template.

Review the work you did in step three of the Django Tutorial

1 Like

Thank you for your support. It worked! :slight_smile: