На фронт нужно отдать такого вида JSON
{
"results": [
{
"title": "Result Title",
"url": "/optional/url/on/click",
"description": "Optional Description"
},
{
"title": "Result Title",
"description": "Result Description"
}
],
// optional action below results
"action": {
"url": '/path/to/results',
"text": "View all 202 results"
}
}
У меня функция следующего вида:
def search_result(request):
if not request.user.is_authenticated():
return redirect('login')
search_str = request.GET['q']
search_str = search_str.replace('.', ' ')
search_str_split = search_str.split(' ')
search_result_data = []
for item in search_str_split:
search_result_data = Task.objects.filter(Q(title__contains=item) |
Q(description__contains=item)) \
.filter(Q(author=request.user) | Q(performer=request.user)) \
.order_by(
'-date_finish')
result = dict()
result['result'] = []
for res in search_result_data:
result['result'].append({'title': res.title, 'url': 'task_det', 'description': res.description})
result['action'] = {"url": '/path/to/results',
"text": "View all 202 results"}
return JsonResponse(result)
Json content получается такой
b'{"result": [{"title": "\\u043f\\u0440\\u0438\\u043a\\u0440\\u0443\\u0442\\u0438\\u0442\\u044c REST API", "url": "task_det", "description": ""}, {"title": "\\u043f\\u0440\\u0438\\u043a\\u0440\\u0443\\u0442\\u0438\\u0442\\u044c REST API", "url": "task_det", "description": ""}], "action": {"url": "/path/to/results", "text": "View all 202 results"}}'