Reputation: 525
I am developing a search engine for our college using Django-Haystack-Solr. I want auto-suggestion and spell check feature. So I used auto_query()
and spelling_suggestion()
method.
This is my views.py
file. As of now, it is only static script not taking any form input from actual user.
from django.shortcuts import render
from haystack.query import SearchQuerySet
import json
from django.http import HttpResponse
def testpage(request):
search_keyword = 'stikar'
data = SearchQuerySet().auto_query(search_keyword)
spelling = data.spelling_suggestion()
sqs = SearchQuerySet().autocomplete(context_auto=spelling)
suggestions = [result.title for result in sqs]
link = [result.url for result in sqs]
context = [result.context for result in sqs]
the_data = json.dumps({
'results': suggestions,
'link': link,
'context': context
})
return HttpResponse(the_data, content_type='application/json')
My main url.py
file:
from django.conf.urls import url, include
from django.contrib import admin
from search.views import testpage
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^test/', testpage), # this is where JSON API is being generated
url(r'^', include('haystack.urls')), # this is where actual search page with search form is
]
I want that JSON response to be called whenever I go to that particular view. I want AJAX request to perform.
P.S: API is being rendered perfectly fine, no issues there. and sorry for bad English.
Please ask me for any extra information.
Thank you. :)
Upvotes: 1
Views: 78
Reputation: 525
I have JSON format something like this:
{
'results': [suggestions],
'link': [link],
'context': [context]
}
This JSON comes from /testpage/
and I want to use that in /testquery/
. Here goes code for testquery.html
let assume that you want to perform a GET method with query param q
.
$.ajax({
type:"GET",
url:"/testpage/",
data:{
'q': <get param value from DOM>
},
success:function(data)
{
console.log(data.results);
console.log(data.link);
console.log(data.context);
}
});
Upvotes: 0