Search code examples
pythondjangoformssearchbar

how to display django search result in new page


i want to display my django search result on a new html page instead of the page where the search bar is. i have tried manipulating my form tag and it still does'nt redirect to the page when i search, rather it stays on the same page.

index.html

<form action="{% url 'elements:all-elements' %}" method="get">
  <input type="text" class="form-control" name="q" value="{{ request.GET.q }}" type="text" placeholder="Search Free Web Elements and Resources">
  <button type="submit" class="btn bt-round" ><b><i class="bi bi-search"></i></b></button>
</form>

views.py - this is the view handling the seach, i dont know if anything would be appended there

# Search Function
    query = request.GET.get("q")
    if query:
        vectors = vectors.filter(
            Q(title__icontains=query)).distinct()

Solution

  • You can render new template for your search results.

    
    def search(request):
        query = request.GET.get("q")
        vectors = Model.objects.all()
        if query:
           vectors = vectors.filter(
                Q(title__icontains=query)).distinct()
        context = {"vectors":vectors}
        return render(request,  "template",  context)