API文档未显示照片视图问题排查:URL注册后无显示无报错
问题排查与解决
核心问题分析
- 类视图与函数视图装饰器冲突:你在
APIView的类方法上使用了@api_view装饰器,这个装饰器仅适用于函数视图,类视图中使用会导致DRF的文档生成器无法识别接口。 - APIView缺少必要工具方法:
APIView本身没有get_object()和get_serializer()方法,这两个方法属于GenericAPIView及其子类,你的代码调用这些方法会在实际请求时触发错误(只是当前未触发到)。 - 类视图未实现标准HTTP方法:DRF文档生成器只会识别类视图中以
get/post/delete命名的方法,你自定义的upload_grounds_driveway_photo方法不会被识别。 - URL未匹配主键参数:方法定义了
pk=None,但URL路径中没有对应参数,导致DELETE等需要主键的请求无法正确匹配。
修正方案(推荐使用ModelViewSet,简化CRUD逻辑)
1. 修正views.py
ModelViewSet会自动封装GET/POST/DELETE等标准CRUD方法,同时支持文档自动生成:
from rest_framework import viewsets from rest_framework.response import Response from rest_framework import status from .models import GroundsDrivewayPhotos from . import serializers from rest_framework.authentication import TokenAuthentication from rest_framework.permissions import IsAuthenticated class GroundsDrivewayPhotosViewSet(viewsets.ModelViewSet): """Manage Grounds Driveway Photos in the database.""" serializer_class = serializers.GroundsDrivewayPhotosSerializer queryset = GroundsDrivewayPhotos.objects.all() authentication_classes = [TokenAuthentication] permission_classes = [IsAuthenticated] def get_queryset(self): """Filter queryset to authenticated user.""" return self.queryset.filter(user=self.request.user).order_by('grounds_driveway_photos') # 自定义上传逻辑(可选,若需关联当前用户) def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) if serializer.is_valid(): serializer.save(user=request.user) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
2. 修正urls.py
使用DRF路由注册器自动生成符合REST规范的URL,确保文档生成器能识别接口:
from django.urls import path, include from rest_framework.routers import DefaultRouter from . import views app_name = 'report' router = DefaultRouter() router.register('photos/grounds-driveway-photos', views.GroundsDrivewayPhotosViewSet, basename='grounds-driveway-photos') urlpatterns = [ path('', include(router.urls)), ]
备选方案(手动实现APIView方法)
如果坚持使用APIView,需手动实现get/post/delete方法,并继承GenericAPIView来使用工具方法:
修正后的views.py
from rest_framework import generics from rest_framework.response import Response from rest_framework import status from .models import GroundsDrivewayPhotos from . import serializers from rest_framework.authentication import TokenAuthentication from rest_framework.permissions import IsAuthenticated class GroundsDrivewayPhotosView(generics.GenericAPIView): """Manage Grounds Driveway Photos""" serializer_class = serializers.GroundsDrivewayPhotosSerializer queryset = GroundsDrivewayPhotos.objects.all() authentication_classes = [TokenAuthentication] permission_classes = [IsAuthenticated] def get(self, request, pk=None): if pk: photo = self.get_object() serializer = self.get_serializer(photo) else: queryset = self.get_queryset() serializer = self.get_serializer(queryset, many=True) return Response(serializer.data) def post(self, request): serializer = self.get_serializer(data=request.data) if serializer.is_valid(): serializer.save(user=request.user) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) def delete(self, request, pk): photo = self.get_object() photo.delete() return Response(status=status.HTTP_204_NO_CONTENT) def get_queryset(self): return self.queryset.filter(user=self.request.user).order_by('grounds_driveway_photos')
修正后的urls.py
需分别配置列表和详情路径:
app_name = 'report' urlpatterns = [ path('photos/grounds-driveway-photos', views.GroundsDrivewayPhotosView.as_view(), name="grounds-driveway-photos-list"), path('photos/grounds-driveway-photos/<int:pk>', views.GroundsDrivewayPhotosView.as_view(), name="grounds-driveway-photos-detail"), ]
验证方法
启动服务后访问api/docs,即可看到对应的grounds-driveway-photos接口(包含GET/POST/DELETE方法),同时可测试各请求方法是否正常工作。
内容的提问来源于stack exchange,提问作者im_up
相关产品推荐
相关产品推荐

