Sunday, October 21, 2018

API Django

Hi all,

i'm really new on Django, trying to create an RESTful API, and getting this error:

curl -i -U Elly:Elly -H "Content-Type: application/json" -X GET http://192.168.100.22:8000/schedule/3/ 


HTTP/1.1 403 Forbidden

Date: Mon, 22 Oct 2018 05:44:40 GMT

Server: WSGIServer/0.2 CPython/3.6.3

Content-Type: application/json

Vary: Accept, Cookie

Allow: OPTIONS

X-Frame-Options: SAMEORIGIN

Content-Length: 58


{"detail":"Authentication credentials were not provided."}


on my settings.py added below
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.permissions.IsAuthenticated'
),
}

views.py

from schedule.models import Schedule
from users.models import User
from rest_framework import exceptions
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.authentication import SessionAuthentication, BasicAuthentication
from rest_framework.permissions import IsAuthenticated

from schedule.serializers import ScheduleSerializer


class ScheduleList(APIView):
authentication_classes = (SessionAuthentication, BasicAuthentication)
permission_classes = (IsAuthenticated,)

def authenticate(self, request):
username = request.META.get('X_USERNAME')
if not username:
return None
try:
user = User.objects.get(username=username)
except:
raise exceptions.AuthenticationFailed('No such user')
return Response(user, None)

queryset = Schedule.objects.all().filter(user=User.objects.all().filter(id=5)[0])
serializer_class = ScheduleSerializer

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


class ScheduleDetail(APIView):
authentication_classes = (SessionAuthentication, BasicAuthentication)
permission_classes = (IsAuthenticated,)

def authenticate(self, request):
username = request.META.get('X_USERNAME')
if not username:
return None
try:
user = User.objects.get(username=username)
except:
raise exceptions.AuthenticationFailed('No such user')
return Response(user, None)

serializer_class = ScheduleSerializer

def get_queryset(self):
return Schedule.objects.all().filter(user=User.objects.all().filter(id=5)[0])

thanks in advance


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAF%3Duzp30Enf%2BE7mWHXG6t2ZAj5YKc11oQkg4HSQOPaHTOO0QsA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment