I'm doing the exercise with the Django tutorial Rest Framework (http://www.django-rest-framework.org/tutorial/1-serialization)
I'm at the stage of creating URLs and I have a problem with getting to the views.
I execute the code:
import snippets from views
I can not import views, will receive:
'module' object has no attribute 'snippet_list'
My code views:
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)
My code URL:
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),
]
It is just an indentation problem, you should change your snippets/views.py to:
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With