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

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.

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.

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?

mdv77:

http://127.0.0.1:8000/search/?q=thalam

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

/search/ is now changed into ‘’'/search_results/```
Screenshot from 2022-07-20 21-36-251131×465 75.1 KB

I got the output.
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 %}
        "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