Home > Back-end >  how to display django search result in new page
how to display django search result in new page

Time:12-16

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"  name="q" value="{{ request.GET.q }}" type="text" placeholder="Search Free Web Elements and Resources">
  <button type="submit"  ><b><i ></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()

CodePudding user response:

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)
  • Related