Getting error while creating multiple get function inside single APIview class

9 Views Asked by At

My view.py

from rest_framework.views import APIView from rest_framework.response import Response from rest_framework import status from rest_framework.decorators import action

class Sample(APIView): http_method_names = ['get', 'post', 'put', 'patch', 'delete']

def get(self, request):
    # Your logic for the default GET method
    response_data = {'message': 'Default GET method'}
    return Response(response_data, status=status.HTTP_200_OK)

def get_fruits(self, request):
    # Your logic for a custom GET method
    fruits = ["apple", "mango", "orange"]
    response_data = {'fruits': fruits}
    return Response(response_data, status=status.HTTP_200_OK)

def get_films(self, request):
    # Your logic for another custom GET method
    films = ["Saj", "don", "Man"]
    response_data = {"fnames": films}
    return Response(response_data, status=status.HTTP_200_OK)

Urls.py

from django.urls import path from .views import Sample from rest_framework.routers import DefaultRouter

router = DefaultRouter() router.register(r'Apiapp', Sample, basename='Sample')

urlpatterns = [ path('sample/get_fruits/', Sample.as_view({'get': 'get_fruits'}), name='custom-endpoint-1'), path('sample', Sample.as_view(), name='get-fruits'), #path('sample/films/', Sample.as_view({'get': 'get_films'}), name='get-films'), ]

Getting this error

0

There are 0 best solutions below