Django Live search With Ajax Problem

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: ""

});

});

I’d start by looking at your console log and the network information in your browser’s developer tools to see what data is being exchanged. It should help identify the cause of the issue.

Also, when posting code here, please enclose all the code for each file between lines consisting of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This will ensure that the code remains formatted properly. You can also go back and edit your original post, putting those lines before and after each block of code.

Hi i changed first the input name from “q” to “term” but now i still get no posts found and when i press enter it takes me to a json page. and i dont see any errors in cmd line, can you please check out the pics.

The issue is here:

See the docs for autocomplete, particularly the section on source. Also see the example on using a remote source.