Skip to content

Commit

Permalink
Merge pull request #210 from gm3dmo/armynumbersearch
Browse files Browse the repository at this point in the history
Adding army number search
  • Loading branch information
gm3dmo authored Aug 20, 2024
2 parents ba51d2e + 4980181 commit e8a6d02
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions cmp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from django.core.paginator import Paginator
from django.shortcuts import get_object_or_404, render, HttpResponse

from django.db.models import Q

from django.contrib.auth.decorators import login_required

from .models import Acknowledgement
Expand Down Expand Up @@ -445,12 +447,14 @@ def search_soldiers(request):
query = request.GET.get('q')
page_number = request.GET.get('page')
if query:
soldiers = Soldier.objects.filter(surname__icontains=query).order_by('surname', 'initials')
soldiers = Soldier.objects.filter(
Q(surname__icontains=query) |
Q(army_number__icontains=query)
).order_by('surname', 'initials')
else:
soldiers = Soldier.objects.all().order_by('surname', 'initials')
paginator = Paginator(soldiers, settings.PAGE_SIZE)
page_obj = paginator.get_page(page_number)
#return render(request, 'cmp/search-soldiers.html', {'soldiers': soldiers})
return render(request, 'cmp/search-soldiers.html', {'page_obj': page_obj})


Expand Down Expand Up @@ -613,15 +617,14 @@ def soldier(request, soldier_id):
def index(request):
if request.method == 'POST':
surname = request.POST.get('name', '')
soldiers = Soldier.objects.filter(surname__icontains=surname).order_by('surname')



# Pagination
soldiers = Soldier.objects.filter(
Q(surname__icontains=surname) |
Q(army_number__icontains=surname)
).order_by('surname')
paginator = Paginator(soldiers, 10) # Show 10 soldiers per page
page_number = request.GET.get('page')
page_obj = paginator.get_page(page_number)

return render(request, 'cmp/soldier-results.html', {'page_obj': page_obj})
else:
return render(request, 'cmp/index.html')
return render(request, 'cmp/index.html')

0 comments on commit e8a6d02

Please sign in to comment.