Add ajax search

This commit is contained in:
Ethanell 2021-12-03 04:34:10 +01:00
parent c56bcc041c
commit 8293b77752
4 changed files with 16 additions and 4 deletions

View file

@ -6,5 +6,6 @@ urlpatterns = [
path("", views.index, name='index'), path("", views.index, name='index'),
path("<int:people_id>/", views.details, name='details'), path("<int:people_id>/", views.details, name='details'),
path("submit/", views.submit, name="submit"), path("submit/", views.submit, name="submit"),
path("edit/<int:people_id>/", views.edit, name="edit") path("edit/<int:people_id>/", views.edit, name="edit"),
path("ajax/search/<str:text>/", views.ajax_search, name="ajax_search")
] ]

View file

@ -1,5 +1,6 @@
from django.contrib.auth.decorators import login_required 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.shortcuts import render
from django.urls import reverse from django.urls import reverse
@ -57,3 +58,7 @@ def edit(request, people_id: int):
form = SubmitPeople(instance=edited_people) form = SubmitPeople(instance=edited_people)
return render(request, "people/edit.html", {"form": form, "edit_id": people_id}) 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)))

View file

@ -6,5 +6,6 @@ urlpatterns = [
path('', views.index, name='index'), path('', views.index, name='index'),
path('<int:rescue_id>/', views.details, name='details'), path('<int:rescue_id>/', views.details, name='details'),
path("submit/", views.submit, name="submit"), path("submit/", views.submit, name="submit"),
path("edit/<int:rescue_id>/", views.edit, name="edit") path("edit/<int:rescue_id>/", views.edit, name="edit"),
path("ajax/search/<str:text>/", views.ajax_search, name="ajax_search")
] ]

View file

@ -1,5 +1,6 @@
from django.contrib.auth.decorators import login_required 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.shortcuts import render
from django.urls import reverse from django.urls import reverse
@ -54,3 +55,7 @@ def edit(request, rescue_id: int):
form = SubmitRescue(instance=edited_rescue) form = SubmitRescue(instance=edited_rescue)
return render(request, "article/edit.html", {"form": form, "edit_id": 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)))