1

I'm using Django Rest Framework to create a non-model API endpoint, but I'm a having a bit of trouble setting it up. Below is my code.

views.py

from rest_framework import views, viewsets
from rest_framework.response import Response

from myproject.apps.policies.models import Customer
from .serializers import CustomerSerializer


class CustomerViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Customer.objects.all()
    serializer_class = CustomerSerializer


class CalculateQuoteView(views.APIView):

    def get(self, request, *args, **kwargs):
        print('Just a random test.')
        return Response({"success": True, "content": "Hello World!"})

My url.py file:

from django.conf.urls import include, url
from .policies import views as policies_views
from rest_framework.routers import DefaultRouter

router = DefaultRouter()
router.register('policies', policies_views.CustomerViewSet)
#urlpatterns = router.urls

urlpatterns = [
    url(r'^quote/', policies_views.CalculateQuoteView.as_view()),
    url(r'^', include(router.urls)),
]

Using curl for testing:

curl -X GET http://localhost:8000/api/v1/policies/quote/ -H 'Authorization: Token 8636c43eb7a90randomtokenhere5c76555e93d3'

I get the following output:

{"detail":"Not found."}

Basically, in the end I will need to pass in details to the quote API endpoint and get some response data. Is there something I'm missing?

2
  • what is the status code? does this print print('Just a random test.') executes while requesting? Commented Aug 24, 2017 at 11:12
  • @ArpitSolanki: Nope. I'm getting the following: Not Found: /api/v1/policies/quote/. Commented Aug 24, 2017 at 11:15

1 Answer 1

2

You should query:

curl -X GET http://localhost:8000/api/v1/quote/

or alter the urls as:

url(r'^policies/quote/', policies_views.CalculateQuoteView.as_view()),
Sign up to request clarification or add additional context in comments.

Comments

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.