0

My code:

api/urls.py

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

urlpatterns = [
    url(r"^entries", views.EntryList.as_view(), name="api-entries-list"),
    url(r"^entries/(?P<pk>[0-9]+)/$", views.EntrySingle.as_view(), name="api-entry")
]

api/views.py

from django.shortcuts import render
from django.http import Http404

from blog.models import Entry
from api.serializers import EntrySerializer

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status

# Create your views here.
class EntryList(APIView):
    def get(self, request, format=None):
        entries = Entry.objects.all()
        serializer = EntrySerializer(entries, many=True)
        return Response(serializer.data)

    def post(self, request, format=None):
        serializer = EntrySerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

class EntrySingle(APIView):
    def get_object(self, pk):
        try:
            return Entry.objects.get(pk=pk)
        except Entry.DoesNotExist:
            raise Http404

    def get(self, request, pk, format=None):
        entry = self.get_object(pk)
        serializer = EntrySerializer(entry)
        return Response(serializer.data)

    def put(self, request, pk, format=None):
        entry = self.get_object(pk)
        serializer = EntrySerializer(entry, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)

    def delete(self, request, pk, format=None):
        entry = self.get_object(pk)
        entry.delete()
        return Response(status=HTTP_204_NO_CONTENT)

api/serializers.py

from blog.models import *
from cvitae.models import *
from activities.models import *

from rest_framework import serializers

class EntrySerializer(serializers.ModelSerializer):
    class Meta:
        model = Entry
        fields = ("title", "slug")

I have an app called blog which contains Entry models having title, content and slug in short. I wanted to use Django Rest Framework to get, put or delete my entries. The code is as above. When I trigger /api/entries, it is okay, it gives all Entry model instances in database.

[
    {
        "title": "And Maybe This is Another One",
        "slug": "and-maybe-this-is-another-one"
    },
    {
        "title": "Another Topic Here",
        "slug": "another-topic-here"
    },
    {
        "title": "An Example Title",
        "slug": "an-example-title"
    }
]

However, when I exactly want to have an Entry instance by a pk value, I still get the same output, which means I still get all objects. I don't know where I did wrong.


Environment

  • django 1.9.5
  • python 3.5.1
  • djangorestframework 3.3.3

1 Answer 1

2

You need to terminate your first regex, otherwise it matches everything beginning with "entries".

r"^entries/$"
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks buddy, you saved me. I can accept this as valid after 4 mins later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.