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()