对于非模型端点,未在Django REST Framework中使用API​​View解析URL

时间:2017-08-24 11:10:04

标签: django python-3.x rest django-rest-framework

我正在使用Django Rest Framework创建一个非模型API端点,但我在设置它时遇到了一些麻烦。以下是我的代码。

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!"})

我的url.py文件:

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)),
]

使用curl进行测试:

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

我得到以下输出:

{"detail":"Not found."}

基本上,最后我需要将详细信息传递给quote API端点并获取一些响应数据。有什么我想念的吗?

1 个答案:

答案 0 :(得分:2)

您应该查询:

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

或将网址更改为:

url(r'^policies/quote/', policies_views.CalculateQuoteView.as_view()),