Hi so i am using Jquery method to do live search , but nothing shows up, here is all i have tried so far…
My Url s File:
from django.urls import path
from .views import Home,Posts,Data,Test,SearchResults
urlpatterns = [
path("", Home, name = "Home"),
path("posts/", Posts, name = "Post"),
path("data/", Data, name = "Data"),
path("test/", Test, name = "Test"),
path('search/', SearchResults, name='search_results'),
]
My Views File:
from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from .models import Post
def SearchResults(request):
`if "term" in` request.POST:
`F = request.POST.get("term")`
`qs = Post.objects.filter(title__icontains = F)`
`title = []`
`for p in qs:`
`title.append(p.title)`
`return JsonResponse(title, safe=False)`
`return render(request,"blog/search_results.html")`
My Search results HTML FILE:
{% extends 'blog/base.html' %}
{% load static %}
{% block content %}
<h1>Search Results</h1>
<form action="{% url 'search_results' %}" method = "POST">
`{% csrf_token %}`
<input name="q" id = "P" type="text" placeholder="Search...">
</form>
{% endblock content %}
{% block js %}
<script defer src="
https://code.jquery.com/jquery-1.12.4.js
"></script>
<script defer src="
https://code.jquery.com/ui/1.12.1/jquery-ui.js
"></script>
<script defer src="{% static 'blog/S.js' %}"> </script>
{% endblock js %}
And Finally My JS File:
$( function() {
$( "#P" ).autocomplete({
source: ""
});
});