我无法在Django休息框架中执行 PUT 操作,我可以进行GET,POST,DELETE操作。
这是错误 我尝试过使用@api_view,但也没有, 我主要推荐djangorestframework网站
以下是我的代码:
serializers.py
from rest_framework import serializers
from snippets.models import Snippet
#from django.contrib.auth.models import *
class SnippetSerializer(serializers.ModelSerializer):
class Meta:
model = Snippet
fields = ('id','title','code')
Models.py
from __future__ import unicode_literals
from django.db import models
# Create your models here.
class Snippet(models.Model):
created = models.DateTimeField(auto_now_add=True)
title = models.CharField(max_length=100, blank=True, default='')
code = models.CharField(max_length=100, blank=True, default='')
class Meta:
ordering = ('created',)
Views.py
from rest_framework import viewsets
from rest_framework.decorators import api_view
from snippets.serializers import SnippetSerializer
class SnippetViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
"""
queryset = Snippet.objects.all().order_by('id')
serializer_class = SnippetSerializer
urls.py
rom django.conf.urls import url, include
from rest_framework import routers
from django.contrib import admin
from snippets import views
router = routers.DefaultRouter()
router.register(r'snippet', views.SnippetViewSet)
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
]
答案 0 :(得分:0)
查看DefaultRouter
的文档,看起来PUT
方法只能与{basename}-detail
类型的网址一起使用。
当您在/snippet
网址上直接使用它时,不允许这样做。