Issue
I'm trying to implement search bar in django, if search item present in Database i'm displaying in web page. But its showing query returns None value. I'm new to django.Please help me in this
views.py
def search(request):
if request.method == 'GET':
form = LocForm(request.POST)
if form.is_valid():
search_query = request.GET.get('search_box', None)
print(search_query)
FirstLoc_list_obj = FirstLoc_List.objects.filter(address__icontains=search_query)
SecondLoc_list_obj= SecondLoc_list.objects.filter(address__icontains=search_query)
if (len(FirstLoc_list_obj) or len(SecondLoc_list_obj)) > 0:
print("Locaton Found")
return render(request, 'location_test.html', {'FirstLoc_list_obj': FirstLoc_list_obj, 'SecondLoc_list_obj': SecondLoc_list_obj})
else:
print("Location Not Found")
return render(request, 'location_test.html', {})
return render(request, 'location_test.html', {})
return render(request, 'location_test.html', {})
urls.py
urlpatterns = [
path('search/', views.search, name="search"),
]
location_test.html
<form type="get" action="#" style="margin: 0">
{% csrf_token %}
<label>Locations:-</label>
<input id="search_box" type="text" name="search_box" placeholder="Search..." >
<button id="search_submit" type="submit" >Submit</button>
</form>
Solution
As I said in comment you cannot use icontains
with None
value. Try this:
def search(request):
if request.method == 'GET':
form = LocForm(request.POST)
if form.is_valid():
search_query = request.GET.get('search_box', None)
if search_query:
FirstLoc_list_obj = FirstLoc_List.objects.filter(address__icontains=search_query)
SecondLoc_list_obj= SecondLoc_list.objects.filter(address__icontains=search_query)
Answered By - Emilio Conte
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.