Rendering Django_Filter Search Results on the searched Property Page

I have tried different options and I don't seem to get how to show my searched property from the home page on searched page. Basically, I have two templates home page and searched property page. I have a search form in my home page and I would like to display the results on the other page.

My views.py for Home Page

def home(request,):


properties = Property.objects.all()[:5]

context = {"property": property, "properties": properties}
return render(request, "base/index.html", context)

My views for Search Page

def prop_search(request,):

    property = Property.objects.all()
    
    properties = Property.objects.all()
    images = Image.objects.filter(property__in=property.all())
   
    propFilter = PropertyFilter(request.GET, queryset=property)
    
    count = propFilter.qs.count()
  
    context = {
        "propFilter": propFilter,
        "property": property,
        "properties": properties,
        "images": images,
        'paginator':paginator,

    }
    return render(request,'base/property-list.html', context)

My Urls.py

path("", views.home, name="home"),
path("add-property", views.addProperty, name="addProperty"),
path("prop_search", views.prop_search, name="prop_search"),

You write that you have a search form on the home page. You can use action to specify where you want to return the result.

<form method="post" action="/your way/">

In this case, the form is called via the 'index3' view via the 'index3.html' template. In action="/bboard/name/" you must write your path to send the result. In the 'name' view, you get the data via request. For example, the request.method method. In this case, I have it POST. Data access via request.POST.get("name"). This opens access to the "name" field. The data is presented in the form of a dictionary. Well, then from this data you can make a dictionary and send it to render.

In my case, the result is sent to the 'name' view, where the resulting name is displayed on the page.

urls

urlpatterns = [

path("index3/", index3),
path("name/", name),
]

views(in index3 write your path to the template)

def index3(request):
   return render(request, "bboard/index3.html")

def name(request):
   print(request)
   name = request.POST.get("name", "Undefined")
   return HttpResponse(f"<div>Name: {name}  <div>")

templates: index3.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
</head>
<body>
    <form method="post" action="/bboard/name/">
        {% csrf_token %}
        <p>Name:<br> <input name="name" /></p>
        <input type="submit" value="Send" />
    </form>
</body>
</html>
Back to Top