You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Django DRF ViewSet时出现404页面未找到错误求助

Fixing the 404 Error with Django DRF ViewSet

Hey there, let's break down why you're hitting that 404 when accessing /list_organisation/ and how to fix it.

The Root Cause

You're using a basic ViewSet (not ModelViewSet), which doesn't come with default implementations for standard actions like list. When you register your ViewSet with the router using router.register(r'list_organisation', OrganisationViewSet), DRF automatically creates a route for the list action at /list_organisation/—but since you haven't defined a list method in your ViewSet, accessing this path throws a 404.

Your custom list_organisations method (decorated with @action) is actually mapped to a different path: /list_organisation/list_organisations/ (DRF appends the method name to the registered route by default for detail=False actions).

Solutions to Try

Option 1: Switch to ModelViewSet (Simplest Approach)

ModelViewSet includes built-in implementations for list, create, retrieve, etc., so you don't need to write custom actions for basic CRUD operations. Update your ViewSet like this:

from rest_framework.viewsets import ModelViewSet

class OrganisationViewSet(ModelViewSet):
    permission_classes = []
    authentication_classes = []
    queryset = Organisation.objects.all()
    serializer_class = OrganisationSerializer  # Make sure this serializer is defined

Now accessing /list_organisation/ will return your organisation list automatically—no extra action needed.

Option 2: Implement the list Method in Your ViewSet

If you want to stick with the basic ViewSet, add a list method to handle the default route:

class OrganisationViewSet(ViewSet):
    permission_classes = []
    authentication_classes = []
    queryset = Organisation.objects.all()

    def list(self, request, *args, **kwargs):
        serializer = OrganisationSerializer(self.queryset, many=True)
        return Response(serializer.data, status=200)

    # Keep your custom action if you still need it
    @action(detail=False, methods=['get'])
    def list_organisations(self,request,*args,**kwargs):
        print('working')
        data = self.queryset
        serializer = OrganisationSerializer(data,many=True)
        return Response(serializer.data,status=200)

This way, /list_organisation/ will use the list method, and your custom action will still be available at /list_organisation/list_organisations/.

Option 3: Map Your Custom Action to the Root Route

If you want your list_organisations action to respond at /list_organisation/, use the url_path parameter in the @action decorator to override the default path:

@action(detail=False, methods=['get'], url_path='')
def list_organisations(self,request,*args,**kwargs):
    print('working')
    data = self.queryset
    serializer = OrganisationSerializer(data,many=True)
    return Response(serializer.data,status=200)

Now this action will handle requests to /list_organisation/ directly.

Quick Check

Double-check that your router is properly included in your URL config—your current code path('',include(router.urls)) is correct, so that's not the issue here.

内容的提问来源于stack exchange,提问作者Atif Shafi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 17:54:08