I added "django.contrib.auth.middleware.LoginRequiredMiddleware" since pretty much all pages should be behind a login only. However when trying to upload a file via API, I get a 404 error as response and when looking in the django logs I see it is forwarded to account/login. I tried to add 'rest_framework.permissions.AllowAny' and also @login_not_required too all functions in the view but same issue. Can I use the loginrequired middleware with Rest_Frameworks?
in the views.py file, I tried adding it like this:
class FileUploadView(APIView):
@login_not_required
def post(self, request, *args, **kwargs):
metadata = {
'file': request.data.get('file'),
}
serializer = FileUploadSerializer(data=metadata)
if serializer.is_valid():
file = serializer.validated_data['file']
s3_client.upload_fileobj(file, settings.AWS_STORAGE_BUCKET_NAME, file.name)
return Response({"message": "{} File uploaded successfully".format(file_name)}, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
in urls.py I simply call it as_view:
urlpatterns = [ path('upload/', FileUploadView.as_view(), name='file-upload')]
the way I try to upload is then via python script calling the url using requests module
You should decorate the result of the .as_view()
method, like:
urlpatterns = [
path(
'upload/',
login_not_required(FileUploadView.as_view()),
name='file-upload',
)
]