Search code examples
reactjsdjangodjango-rest-frameworkaxiosdjango-views

TypeError: SearchProduct.get() got an unexpected keyword argument 'query'


in windows 10 , i'm using react-router-dom 5.2.0 and react-redux 7.2.5 and react 17.0.2 and axios 0.21.4 and WebStorm 2023.1.3 IDE and PyCharm Community Edition 2023.2 and djangorestframework==3.14.0 and Django==4.2.4 and djangorestframework-simplejwt==5.3.0.

question : Actually, I don't know how to send this query parameter to Django's base class view that inherits from GenericAPIView , how solve this error ?

BACKEND:

Consider - product_views.py :

class SearchProduct(GenericAPIView):

    serializer_class = ProductSerializer
    pagination_class = CustomPagination1

    def get_queryset(self, *args, **kwargs):
        # the lead id
        query = self.request.GET.get("query")

        #  this filter base on the lead id  provided
        lookup = Q(name__icontains=query) | Q(description__icontains=query) | Q(producttag__title__icontains=query)
        products = Product.objects.filter(lookup).distinct()

        return products
    def get(self, request):


        page = self.paginate_queryset(self.get_queryset())
        if page is not None:
            serializer = self.get_serializer(page, many=True)
            result = self.get_paginated_response(serializer.data)
            data = result.data  # pagination data

        else:
            serializer = self.get_serializer(queryset, many=True)
            data = serializer.data
        payload = {
            'return_code': '0000',
            'return_message': 'Success',
            'data': data
        }
        return Response(data , status=status.HTTP_200_OK)

Consider - product_urls.py:

 path('search_product/<str:query>/' , views.SearchProduct.as_view() , name="search_product"),

FRONTEND:

Consider - productAction.py:

export const productsSearchAction = (query , pageNumber) => async (dispatch , getState) => {
    try {
        dispatch({type: PRODUCTS_SEARCH_REQUEST});
        const {data} = await axios.get(`http://127.0.0.1:8000/api/v1/products/search_product/${query}/?page=${pageNumber}`);
        dispatch({type: PRODUCTS_SEARCH_SUCCESS , payload: data});
        localStorage.setItem("productsSearch" , JSON.stringify(data));
    } catch (error) {
        dispatch({ // PRODUCTS SEARCH FAILED
            type: PRODUCTS_SEARCH_FAILED,
            payload: error.response && error.response.data.detail ? error.response.data.detail : error.message,
        });
    }
}

I think the answer is simple, if you are an expert in Django and Django Rest Framework, please reply to me so that this error can be solved.

my error screen:

enter image description here


Solution

  • Your .get(…) method will need to accept the URL path parameters, here you can better just accept all positional and named parameters:

    class SearchProduct(GenericAPIView):
        serializer_class = ProductSerializer
        pagination_class = CustomPagination1
    
        def get(self, request, *args, **kwargs):
            # …

    Note: In Django, class-based API views (CBV) often have a …APIView suffix, to avoid a clash with the model names. Therefore you might consider renaming the view class to SearchProductAPIView, instead of SearchProduct.