Django Rest:视图不想导入

Django Rest: The view does not want to import

我正在使用 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),
]

这只是一个缩进问题,你应该把你的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)