Hi,
I have the following url.py file:
from django.urls import path
from api.views import SupplierList, SearchResultsView
urlpatterns = [
path('suppliers/', SupplierList.as_view(), name='supplier-list'),
path('suppliers/<str:query>/', SearchResultsView.as_view(), name='search-results'),
]
I also have the following views.py file:
from django.shortcuts import render
from rest_framework import generics
from api.models import Supplier
from api.serializers import SupplierSerializer
from api.pagination import *
from rest_framework.response import Response
from rest_framework import status
class SearchResultsView(generics.ListAPIView):
serializer_class = SupplierSerializer
def get_queryset(self):
query = self.request.query_params.get('query')
print(query)
object_list = Supplier.objects.filter(name__icontains=query)
return object_list
class SupplierList(generics.ListCreateAPIView):
queryset = Supplier.objects.all()
serializer_class = SupplierSerializer
pagination_class = SmallResultsSetPagination
def create(self, request, *args, **kwargs):
try:
serializer = self.get_serializer(data=request.data, many=True)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
except Exception as e:
print(e)
return Response({}, status=status.HTTP_400_BAD_REQUEST)
Every time I make a search request, it keeps taking me to the ‘supplier-list’ as opposed to ‘search-results’ url.
Can someone please guide me as to what I am doing wrong?