0
我無法做PUT在Django rest框架中運行,我能夠通過GET,POST,DELETE操作。無法執行PUT操作-DjangoRESTFramework
這是我一直在使用@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')),
]
我必須輸入{basename} -detail的地方。我讀了文檔,但沒有得到它 –
這意味着你可以在已經創建的特定片段上使用'PUT'方法。因此,我們假設該片段的ID爲「123」,那麼您可以使用'/ snippet/123'嘗試訪問它,然後在此URL上使用PUT方法。 – AKS