Django REST API 404

Hi,
I’m trying to implement Django’s REST Api. I followed the tutorial and everything compiles fine and all other parts of the app work but when I try to access the url at http://app.url/players I get the page not found. Can anyone help me with this?

Thanks in advance.

PS: if I try to create the url in the normal manner with a path statement I get an error that I supplied 2 positional arguments instead of 1. So it looks like 2 problems perhaps.

serializers.py

from .models import Player
from rest_framework import serializers

class PlayerSerializer(serializers.HyperlinkedModelSerializer):
  class Meta:
    model = Player
    fields = ['name', 'gp']

views.py

from rest_framework import viewsets
from rest_framework import permissions
from Striker.serializers import PlayerSerializer

urls.py

from django.contrib import admin
from django.urls import path, include
from rest_framework import routers
from Striker import views

router = routers.DefaultRouter()
router.register(r'players/', views.PlayerViewSet)

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('Striker.urls')),
    path('api-auth/', include('rest_framework.urls'), name='rest_framework')
]

class PlayerViewSet(viewsets.ModelViewSet):
queryset = Player.objects.all().order_by(‘-gp’)
serializer_class = PlayerSerializer
permission_class = [permissions.IsAuthenticated]

models.py

class Player(models.Model):
name = models.CharField(max_length=30)
gp = models.IntegerField()

You are registering the url:

But you’re trying to access:

The two are not the same. (You’re missing the trailing slash on the url.)

Thanks for the reply but I get the 404 whether or not I supply a trailing slash on the url. I also tried removing the trailing slash from the router.register statement but same problem.

Ahh, I also don’t see where you are mapping your router urls in your urlpatterns. (See the example at Quickstart - Django REST framework)

“Because we’re using viewsets instead of views, we can automatically generate the URL conf for our API, by simply registering the viewsets with a router class”

Isn’t that what my code does?

(That is the doc I’m looking at.)

OK, I just added ‘urlpatterns += router.urls’ and it’s all good. Thanks for your patience, Ken.