我有一个视图和序列化器:
class UserView(generics.RetrieveUpdateAPIView):
model = get_user_model()
serializer_class = UserProfileSerializer
permission_classes = (permissions.IsAuthenticated,)
def get_object(self, *args, **kwargs):
return self.request.user
class UserImageSerializer(serializers.ModelSerializer):
class Meta:
model = get_user_model()
fields = ('image',)
他们与httpie合作很好:
http -f put localhost:8000/accounts/api/image/ "Authorization: Token mytoken" image@~/Downloads/test.jpg
HTTP/1.0 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Date: Thu, 03 Sep 2015 22:50:33 GMT
Server: WSGIServer/0.2 CPython/3.4.3
Vary: Accept
X-Frame-Options: SAMEORIGIN
{
"image": "http://localhost:8000/media/accounts/user_images/test.jpg"
}
并且我的图片已上传并显示在管理员中.
现在我希望能够使用AJAX上传文件,但显然不想工作:
method="put"
enctype="multipart/form-data">
$('form').submit(function(e) {
var formData = new FormData($(this));
$.ajax({
url: $(this).attr('action'),
type: $(this).attr('method'),
data: formData,
headers: {'Authorization': 'Token mytoken'},
cache: false,
contentType: false,
processData: false,
success: function() { alert('it works') },
});
e.preventDefault();
});
现在,我的"It works"警报出现了.我知道表单正在提交到正确的位置,我可以在Django dev服务器中看到它被请求作为PUT并且它以200响应(与httpie相同的响应):
[03/Sep/2015 22:47:23] "PUT /accounts/api/image/ HTTP/1.1" 200 77
但似乎文件没有被上传,并且它没有显示在管理员中.
我没有想法.