1👍
✅
You can try this.
from django.http import HttpResponse
from django.template.loader import render_to_string
def search_query(request):
var_1 = Products.objects.all()
var_2 = request.GET.get("q")
if var_2:
items = items.filter(
Q(First__icontains=var_2) |
Q(Last__icontains=var_2)
)
response = render_to_string('templates/template_name.html', {'items': items})
return HttpResponse(response)
template_name.html
<table width='100%'>
<thead>
<tr>
<th>xxxxx</th>
<th>YYYYY</th>
</tr>
</thead>
<tbody>
{% for item in items %}
<tr>
<td><b>{{ item.xxxx }}</b></td>
<td><b>{{ item.yyyyyy }}</b></td>
......................
.......................
</tr>
{% endfor %}
</tbody>
</table>
javascript.js
.........
.........
var search_query = $('#id_search_q').val();
$.ajax({ //insert AJAX
type: "GET",
url "api/search-query/",
data: {'q': search_query //take querystring
},
success:function(response){ //activate javascript
$("#welcomeDiv").html(response)
}
........
........
index.html
<form method="GET" action="">
<input type='text' id='id_search_q' name='q' value='{{ request.GET.q }}'></input>
<input type='submit' onclick='showDiv()'></input>
</form>
urls.py
url('^api/search-query/$', 'views.search_query', name='api_search_query')
Source:stackexchange.com