From 8293b777527dff8dc93947a8491abf330573614a Mon Sep 17 00:00:00 2001 From: flifloo Date: Fri, 3 Dec 2021 04:34:10 +0100 Subject: [PATCH] Add ajax search --- people/urls.py | 3 ++- people/views.py | 7 ++++++- rescue/urls.py | 3 ++- rescue/views.py | 7 ++++++- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/people/urls.py b/people/urls.py index 0a07d07..dca8088 100644 --- a/people/urls.py +++ b/people/urls.py @@ -6,5 +6,6 @@ urlpatterns = [ path("", views.index, name='index'), path("/", views.details, name='details'), path("submit/", views.submit, name="submit"), - path("edit//", views.edit, name="edit") + path("edit//", views.edit, name="edit"), + path("ajax/search//", views.ajax_search, name="ajax_search") ] diff --git a/people/views.py b/people/views.py index 5687eab..827d59e 100644 --- a/people/views.py +++ b/people/views.py @@ -1,5 +1,6 @@ from django.contrib.auth.decorators import login_required -from django.http import Http404, HttpResponseRedirect, HttpResponseBadRequest +from django.db.models import Q +from django.http import Http404, HttpResponseRedirect, HttpResponseBadRequest, JsonResponse from django.shortcuts import render from django.urls import reverse @@ -57,3 +58,7 @@ def edit(request, people_id: int): form = SubmitPeople(instance=edited_people) return render(request, "people/edit.html", {"form": form, "edit_id": people_id}) + + +def ajax_search(request, text: str): + return JsonResponse(People.objects.filter(Q(first_name__icontains=text) | Q(first_name__icontains=text))) diff --git a/rescue/urls.py b/rescue/urls.py index 42a7fe9..79ba399 100644 --- a/rescue/urls.py +++ b/rescue/urls.py @@ -6,5 +6,6 @@ urlpatterns = [ path('', views.index, name='index'), path('/', views.details, name='details'), path("submit/", views.submit, name="submit"), - path("edit//", views.edit, name="edit") + path("edit//", views.edit, name="edit"), + path("ajax/search//", views.ajax_search, name="ajax_search") ] diff --git a/rescue/views.py b/rescue/views.py index 9e05b8f..9c329cb 100644 --- a/rescue/views.py +++ b/rescue/views.py @@ -1,5 +1,6 @@ from django.contrib.auth.decorators import login_required -from django.http import Http404, HttpResponseRedirect, HttpResponseBadRequest +from django.db.models import Q +from django.http import Http404, HttpResponseRedirect, HttpResponseBadRequest, JsonResponse from django.shortcuts import render from django.urls import reverse @@ -54,3 +55,7 @@ def edit(request, rescue_id: int): form = SubmitRescue(instance=edited_rescue) return render(request, "article/edit.html", {"form": form, "edit_id": edited_rescue}) + + +def ajax_search(request, text: str): + return JsonResponse(Rescue.objects.filter(Q(name__icontains=text) | Q(date__icontains=text) | Q(resume__icontains=text)))