1
votes

I am trying to create an api using django rest framework. I need one view to list all items in the datatable and another view that outputs the data for an individual person based on their id.

The list view works at 'api/bios/'. However, I receive the following error when adding an id to the url:

HTTP 404 Not Found Allow: GET, PUT, PATCH, DELETE, HEAD, OPTIONS Content-Type: application/json Vary: Accept

{ "detail": "Not found." }

views.py

from rest_framework.response import Response
from rest_framework import status
from rest_framework import generics, mixins
from api.models import Playerbios
from api.serializers import USASerializer

class USAListView(mixins.CreateModelMixin, generics.ListAPIView):
    lookup_field = 'trackmanid'
    serializer_class = USASerializer

    def get_queryset(self):
        return Playerbios.objects.all()

    def perform_create(self, serializer):
        serializer.save(user=self.request.user)

    def post(self, request, *args, **kwargs):
        return self.create(request, *args, **kwargs)


class USAView(generics.RetrieveUpdateDestroyAPIView):
    lookup_field = 'trackmanid'
    serializer_class = USASerializer

    def get_queryset(self):
        return Playerbios.objects.all()

serializers.py

from rest_framework import serializers
from api.models import Playerbios

class USASerializer(serializers.ModelSerializer):
    class Meta:
        model = Playerbios
        fields = '__all__'
        lookup_field = 'trackmanid'

main/urls.py

from django.contrib import admin
from django.urls import path, include
from api.views import USAView, USAListView

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/bios/', include('api.urls')),
    path('api/bios/<trackmanid>/', include('api.urls')),
    path('accounts/', include('django.contrib.auth.urls')),
    path('', include('webapp.urls')),

]

api/urls.py

from django.conf.urls import url, include
from .views import USAView, USAListView

urlpatterns = [
    url('(?P<trackmanid>)/', USAView.as_view(), name='usa-rud-view'),
    url('', USAListView.as_view(), name='usa-list-view'),
]
2
add ^ (caret sign) in start and $(dollar sign) in the end of url's regex in api/urls.py Then your url's should be : url('^(?P<trackmanid>)/$', USAView.as_view(), name='usa-rud-view'), url('^$', USAListView.as_view(), name='usa-list-view'),Ganesh Negi

2 Answers

2
votes

Have you considered using Viewsets for this API? DRF has a powerful ViewSet that performs all of these functions with just minimal configuration. For example, this can be re-written as:

views.py

class USAViewset(ModelViewSet):
    queryset = Playerbios.objects.all()
    serializer_class = USASerializer
    lookup_field = 'trackmanid'

main urls.py

urlpatterns = [
    ...
    path('api/bios/', include('api.urls')),
    ...    
]

api/urls.py

urlpatterns = patterns(
    '',
)
router = DefaultRouter()
router.register(r'coupon', USAViewset)
urlpatterns += router.urls

DRF will do the rest! Read more here: http://www.django-rest-framework.org/api-guide/viewsets/

0
votes

My guess is that you need to remove this line from your main urls.py file

path('api/bios/<trackmanid>/', include('api.urls'))

Also change your api/urls.py to include the regex. The format is ?P<name>[regex]. Assuming trackmanid is an integer, you need to include a \d+ regex next to it.

url('(?P<trackmanid>\d+)/', USAView.as_view(), name='usa-rud-view'),