本文介绍了在具有视图集的Django Rest框架上执行POST会返回"405-未允许方法发布";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的IOS应用创建一个HiScore模块.

I'm trying to create a HiScore Module for my IOS Apps.

我卡在POST上(可能与PUT会有相同的问题,但不确定),我在Postman中得到405.列表和详细信息运行良好,因此模型,序列化程序或视图没有问题,这只是我猜测的在某处添加权限的问题

I'm stuck on the POST (probably will have the same issue with the PUT, but not sure) part, I'm getting 405 in Postman.LIST AND DETAILS are working great, so there is no problem with the models, serialisers or views, just something I'm guessing with adding permissions somewhere

观看次数

class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer

class GameViewSet(viewsets.ModelViewSet):
    queryset = HiScore.objects.all()
    serializer_class = GameSerializer

class GameTypeViewSet(viewsets.ModelViewSet):
    queryset = HiScore.objects.all()
    serializer_class = GameTypeSerializer

class GameLevelViewSet(viewsets.ModelViewSet):
    queryset = HiScore.objects.all()
    serializer_class = GameLevelSerializer

class HiScoreViewSet(viewsets.ModelViewSet):
    permission_classes = [AllowAny,]
    queryset = HiScore.objects.all()
    serializer_class = HiScoreSerializer

serialisers.py

serialisers.py

class HiScoreSerializer(ModelSerializer):
image = SerializerMethodField()
class Meta:
    model = HiScore
    fields = ("user", "game", "gameType", "gameLevel", "points", "image")

def get_image(self, obj):
    try:
        profile = UserProfile.objects.get(user=obj.user)
        return profile.image.url
    except:
        return None

urls.py

router = DefaultRouter()
router.register(prefix='hiscores', viewset=HiScoreViewSet)

urlpatterns = router.urls

推荐答案

我认为您根据文档.试试这个:

I think you are missing as_view in view according to documentation. Try this:

在视图中:

hiscores = HiScoreViewSet.as_view({
    'get': 'list',
    'post': 'create'
})

并在网址中:

from rest_framework.urlpatterns import format_suffix_patterns
urlpatterns = format_suffix_patterns([
    url(r'^hiscores/$', hiscores, name='hiscores')
])

http://www.django-rest-framework.org/api-guide/viewsets/

这篇关于在具有视图集的Django Rest框架上执行POST会返回"405-未允许方法发布";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 04:36