2016-07-04 16 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