tutorial - python rest framework



Django Rest:視圖不想導入 (1)

這只是一個縮進問題,你應該改變你的snippets / views.py到:

from .models import Snippet
from serializers import SnippetSerializer
from rest_framework.renderers import JSONPRenderer
from rest_framework.parsers import JSONParser
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt

class JSONResponse(HttpResponse):

    def __init__(self, data, **kwargs):
        content = JSONPRenderer().render(data)
        kwargs['content_type'] = 'aplication/json'
        super(JSONPRenderer, self).__init__(content, **kwargs)

@csrf_exempt
def snippet_list(request):
    """
    List all code snippets or create new code snippet
    """
    if request.method == "GET":
        snippet = Snippet.objects.all()
        serializer = SnippetSerializer(snippet, many=True)
        return JSONResponse(serializer.data)

    elif request.method == "POST":
        data = JSONParser().parse(request)
        serializer = SnippetSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data, status=201)
        return JSONResponse(serializer.data, status=400)

@csrf_exempt
def snippet_detail(request, pk):
    """
    Retrive, delete or update code snippet
    """
    try:
        snippet = Snippet.objects.get(pk=pk)
    except Snippet.DoesNotExist:
        return HttpResponse(status=404)

    if request.response == "GET":
        serializer = SnippetSerializer(snippet)
        return JSONResponse(serializer.data)

    elif request.method == "PUT":
        data = JSONParser().parse(request)
        serializer = SnippetSerializer(snippet, data=data)
        if serializer.is_valid():
            serializer.save()
            return HttpResponse(serializer.data)
        return HttpResponse(serializer.errors, status=400)

    elif request.method == "DELETE":
        snippet.delete()
        return HttpResponse(status=204)

我正在使用Django教程Rest Framework進行練習( http://www.django-rest-framework.org/tutorial/1-serialization

我正在創建URL的階段,我有一個問題,得到的意見。

我執行代碼:

import snippets from views

我無法導入視圖,將收到:

'module' object has no attribute 'snippet_list'

我的代碼意見:

from .models import Snippet
from serializers import SnippetSerializer
from rest_framework.renderers import JSONPRenderer
from rest_framework.parsers import JSONParser
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt

class JSONResponse(HttpResponse):

    def __init__(self, data, **kwargs):
        content = JSONPRenderer().render(data)
        kwargs['content_type'] = 'aplication/json'
        super(JSONPRenderer, self).__init__(content, **kwargs)

    @csrf_exempt
    def snippet_list(request):
        """
        List all code snippets or create new code snippet
        """
        if request.method == "GET":
            snippet = Snippet.objects.all()
            serializer = SnippetSerializer(snippet, many=True)
            return JSONResponse(serializer.data)

        elif request.method == "POST":
            data = JSONParser().parse(request)
            serializer = SnippetSerializer(data=data)
            if serializer.is_valid():
                serializer.save()
                return JSONResponse(serializer.data, status=201)
            return JSONResponse(serializer.data, status=400)

    @csrf_exempt
    def snippet_detail(request, pk):
        """
        Retrive, delete or update code snippet
        """
        try:
            snippet = Snippet.objects.get(pk=pk)
        except Snippet.DoesNotExist:
            return HttpResponse(status=404)

        if request.response == "GET":
            serializer = SnippetSerializer(snippet)
            return JSONResponse(serializer.data)

        elif request.method == "PUT":
            data = JSONParser().parse(request)
            serializer = SnippetSerializer(snippet, data=data)
            if serializer.is_valid():
                serializer.save()
                return HttpResponse(serializer.data)
            return HttpResponse(serializer.errors, status=400)

        elif request.method == "DELETE":
            snippet.delete()
            return HttpResponse(status=204)

我的代碼網址:

from django.conf.urls import url
from snippets import views


urlpatterns = [
    url(r'snippets/$', views.snippet_list),
    url(r'snippets/(?P<pk>[0-9]+)/$', views.snippet_detail),
]




django-rest-framework