API Django

70 views
Skip to first unread message

Phako Perez

unread,
Oct 22, 2018, 1:52:50 AM10/22/18
to django...@googlegroups.com
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


Vinod Kumar

unread,
Oct 22, 2018, 2:02:51 AM10/22/18
to django...@googlegroups.com
Remove Isauthenticated from rest_framework in settings.py

--
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...@googlegroups.com.
To post to this group, send email to django...@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.

************************************************************************

This e-mail and all attachments are intended solely for use by the intended recipient and may contain confidential / proprietary information of KiwiTech, LLC, subject to important disclaimers and conditions including restrictions on the use, disclosure, transfer or export of such information. If you have received this message in error or are not the named recipient(s), please immediately notify the sender at the telephone number stated above or by reply e-mail and delete this e-mail from your computer

Phako Perez

unread,
Oct 22, 2018, 2:14:54 AM10/22/18
to django...@googlegroups.com
Thanks Vinod for your quick response, I did, in addition I used -U (capital) instead (-u), however I got error like

Method GET not allowed.

Method Not Allowed: /schedule/3/

[22/Oct/2018 06:13:50] "GET /schedule/3/ HTTP/1.1" 405 40 

Reply all
Reply to author
Forward
0 new messages