Discussion:
API Django
Phako Perez
2018-10-22 05:52:09 UTC
Permalink
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+***@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.
'Vinod Kumar' via Django users
2018-10-22 06:02:06 UTC
Permalink
Remove Isauthenticated from rest_framework in settings.py
Post by Phako Perez
Hi all,
i'm really new on Django, trying to create an RESTful API, and getting
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
authentication_classes = (SessionAuthentication, BasicAuthentication)
permission_classes = (IsAuthenticated,)
username = request.META.get('X_USERNAME')
return None
user = User.objects.get(username=username)
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
serializer.save(user=self.request.user)
authentication_classes = (SessionAuthentication, BasicAuthentication)
permission_classes = (IsAuthenticated,)
username = request.META.get('X_USERNAME')
return None
user = User.objects.get(username=username)
raise exceptions.AuthenticationFailed('No such user')
return Response(user, None)
serializer_class = ScheduleSerializer
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
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
<https://groups.google.com/d/msgid/django-users/CAF%3Duzp30Enf%2BE7mWHXG6t2ZAj5YKc11oQkg4HSQOPaHTOO0QsA%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
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
--
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/CAJOHC1wHOa0-2G%2B8RHrvWi4JzX0-UeX7w%3DAVJ%3DEb_4PHT4OiZw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Phako Perez
2018-10-22 06:14:13 UTC
Permalink
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

On Mon, Oct 22, 2018 at 1:02 AM 'Vinod Kumar' via Django users <
Post by 'Vinod Kumar' via Django users
Remove Isauthenticated from rest_framework in settings.py
Post by Phako Perez
Hi all,
i'm really new on Django, trying to create an RESTful API, and getting
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
authentication_classes = (SessionAuthentication, BasicAuthentication)
permission_classes = (IsAuthenticated,)
username = request.META.get('X_USERNAME')
return None
user = User.objects.get(username=username)
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
serializer.save(user=self.request.user)
authentication_classes = (SessionAuthentication, BasicAuthentication)
permission_classes = (IsAuthenticated,)
username = request.META.get('X_USERNAME')
return None
user = User.objects.get(username=username)
raise exceptions.AuthenticationFailed('No such user')
return Response(user, None)
serializer_class = ScheduleSerializer
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
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
<https://groups.google.com/d/msgid/django-users/CAF%3Duzp30Enf%2BE7mWHXG6t2ZAj5YKc11oQkg4HSQOPaHTOO0QsA%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
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*
--
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
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/CAJOHC1wHOa0-2G%2B8RHrvWi4JzX0-UeX7w%3DAVJ%3DEb_4PHT4OiZw%40mail.gmail.com
<https://groups.google.com/d/msgid/django-users/CAJOHC1wHOa0-2G%2B8RHrvWi4JzX0-UeX7w%3DAVJ%3DEb_4PHT4OiZw%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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%3Duzp3WH3SYKNg979U5H%2B2F_ao4bk1BAb11T7dSy3K0ZD4Ckg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Loading...