Home > Software engineering >  Django get() got an unexpected keyword argument 'slug'
Django get() got an unexpected keyword argument 'slug'

Time:10-18

I need to use Korean in the url, but it doesn't work.

enter image description here

views.py

class BuildingInfoAPI(APIView):
    def get(self, request):
        queryset = BuildingData.objects.all()
        serializer = BuildingSerializer(queryset, many=True)
        return Response(serializer.data)
class ReviewListAPI(APIView):
    def get(self, request):
        queryset = ReviewData.objects.all()
        serializer = ReviewSerializer(queryset, many=True)
        return Response(serializer.data)

urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/buildingdata/', BuildingInfoAPI.as_view()),
    path('api/buildingdata/<str:slug>/', ReviewListAPI.as_view())
]

I've tried slug:slug and url with re_path, but these methods says 'page not found'

so I tried str:slug but it says

get() got an unexpected keyword argument 'slug'

This is slug data in my model.

slug = models.SlugField(max_length=50, unique=True, allow_unicode=True, default=uuid.uuid1)

'allow_unicode' allows using Korean in the url.

I can't find which code is wrong.

Is there any problem with views.py or urls.py?

CodePudding user response:

For the ReviewListAPI, the .get(…) method needs to accept self, request and slug as parameters:

class ReviewListAPI(APIView):
    def get(self, request, slug):
        queryset = ReviewData.objects.all()
        serializer = ReviewSerializer(queryset, many=True)
        return Response(serializer.data)

Since your .get(…) method takes a slug, you should likely rewrite the logic and take the slug into account.

  • Related