2016-07-04 60 views
0

我的API:以上为什么rest_framework.authentication.BasicAuthentication没有在我的代码中工作?

from rest_framework.authentication import BasicAuthentication 
"""A simple API for file upload.""" 
class FileUploadView(APIView): 
    parser_classes = (MultiPartParser,) 
    authentication_classes = (BasicAuthentication,) 
    @method_decorator(csrf_exempt) 
    def dispatch(self, request, *args, **kwargs): 
     return super(FileUploadView, self).dispatch(request, *args, **kwargs) 

    def put(self, request): 
     print "request:", str(request.META) 
     print "request:", str(request.user.username) 
     try: 
      data = {'files': 'testing'} 
      response = Response(data) 
     except Exception as e: 
      print "Exception when put file:", e 
      data = { 'error' : str(e) } 
      response = Response(data) 

     return response 

是我的API views.py。我用邮递员做PUT。我没有在头部授权中添加任何内容(请求标头中没有HTTP_AUTHORIZATION),我可以得到{'files':'testing'}作为我的回应。

为什么?有什么遗漏?谢谢

回答

0

您添加了认证类,但没有限制访问您的视图。默认情况下,DRF具有不受限制的访问权限。请参阅相关文档部分:

如果没有指定,这个默认设置为允许不受限制的访问:

'DEFAULT_PERMISSION_CLASSES': (
    'rest_framework.permissions.AllowAny', 
) 

Setting the permission policy

相关问题