django图像上传休息框架和测试客户端

时间:2016-10-02 13:56:53

标签: python django django-rest-framework

我尝试使用rest框架上传图片并使用django测试客户端进行测试。它几乎可以工作,但是当我保存文件时,我将它放在我的文件顶部:

class RealTimeVC : UIViewController, WCSessionDelegate{
   var session : WCSession!
   override func viewWillAppear(animated: Bool)
    {
        if (WCSession.isSupported())
        {
            session = WCSession.defaultSession()
            session.delegate = self
            session.activateSession()

        }
    }

    //WCSessionProtocol

    func session(session: WCSession,
                 activationDidCompleteWith activationState: WCSessionActivationState,
                 error: NSError?){
        print("Print Something")
    }

    func sessionDidBecomeInactive(session: WCSession){
         print("Print Something")
    }

    func sessionDidDeactivate( session: WCSession){
         print("Print Something")
    }

}

如果我删除这些线条,我可以完美地阅读我的图片,但我不知道如何摆脱它们。

models.py

>>> import os
>>> os.system('date')
Sun Oct  2 19:20:29 IST 2016  
0

views.py

--BoUnDaRyStRiNg
Content-Disposition: form-data; name="plop.png"

tests.py

class GenericUser(User):
    avatar = models.ImageField(upload_to=user_directory_path)

任何提示,我如何才能使这项工作?

2 个答案:

答案 0 :(得分:0)

试试这个:

.cmd

或者更多或更少来自Django文档:

res = cl.post('/avatar?filename=plop.png', {"plop.png" : data})

答案 1 :(得分:0)

这符合我的预期: views.py

class AvatarView(APIView):
    parser_classes = (MultiPartParser,)
    def post(self, request):
        up_file = request.data["file"]
        user = get_object_or_404(GenericUser, pk=request.user.id)
        user.avatar.save(up_file.name, up_file)
        user.save()
        return Response(status=204)

test.py

cl = Client(HTTP_AUTHORIZATION="Token {}".format(token))
with open(MY_FILE, 'rb') as f:
    res = cl.post('/avatar', {"file" : f})