我正在尝试从python脚本上传图像到Django。 我尝试了一些我找到的解决方案但没有效果。
这是我的简化模型:
class ExampleModel(models.Model): address = models.CharField( _('address'), max_length=200, blank=True, ) photo = models.ImageField( verbose_name='photo', upload_to=path_for_photo, null=True, blank=True, validators=[FileValidator(max_size=settings.MAX_PHOTO_SIZE, allowed_mimetypes=settings.ALLOWED_CONTENT_TYPES, allowed_extensions=settings.ALLOWED_EXTENSIONS),] )现在,我的观点是这样的:
def update(self, request, *args, **kwargs): instance = self.get_object() serializer = self.get_serializer(instance, data=request.data, partial=partial) serializer.is_valid(raise_exception=True) self.perform_update(serializer) return Response(serializer.data)这是我在Python中的脚本:
with open('test.jpg', 'rb') as payload: headers = {'content-type': 'application/x-www-form-urlencoded'} files = { 'file': payload, 'Content-Type': 'image/jpeg' } r = requests.put('http://localhost:8000/v1/myendpoint', auth=('example@example.com', 'example'), verify=False, files=files, headers=headers)但我不知道如何修改我的更新方法来更新图像。 我尝试了一些方法:将其作为json发送(更改'content-type'并在requests.put的'data'参数中发送信息),将其作为InMemoryUploadedFile发送,以及我发现googleing的其他一些解决方案周围。
我从来没有想过从Python上传一个简单的图像会那么难! 我可以在html表单中轻松地做同样的事情,但我需要从python脚本中添加它。
I am having a bad time trying to upload an image to Django from a python script. I tried some solutions I found around but none works.
This is my simplified model:
class ExampleModel(models.Model): address = models.CharField( _('address'), max_length=200, blank=True, ) photo = models.ImageField( verbose_name='photo', upload_to=path_for_photo, null=True, blank=True, validators=[FileValidator(max_size=settings.MAX_PHOTO_SIZE, allowed_mimetypes=settings.ALLOWED_CONTENT_TYPES, allowed_extensions=settings.ALLOWED_EXTENSIONS),] )Now, my view is like this:
def update(self, request, *args, **kwargs): instance = self.get_object() serializer = self.get_serializer(instance, data=request.data, partial=partial) serializer.is_valid(raise_exception=True) self.perform_update(serializer) return Response(serializer.data)This is the script I have in Python:
with open('test.jpg', 'rb') as payload: headers = {'content-type': 'application/x-www-form-urlencoded'} files = { 'file': payload, 'Content-Type': 'image/jpeg' } r = requests.put('http://localhost:8000/v1/myendpoint', auth=('example@example.com', 'example'), verify=False, files=files, headers=headers)But I don't know how to modify my update method to update images. I've tried a few approaches: send it as json (changing the 'content-type' and sending the info in 'data' parameter of requests.put), send it as InMemoryUploadedFile, and some other solutions that I've found googleing around.
I never thought that upload a simple image from Python would be that hard! I could do the same easily in a html form, but I need to put it from a python script.
最满意答案
尝试这个:
with open('test.jpg', 'rb') as payload: files = { 'photo': payload, 'Content-Type': 'image/jpeg' } r = requests.put( 'http://localhost:8000/sendfiles/1/', verify=False, files=files ) print(r)请求将看到“files”参数具有内容并将相应地设置标头。
Try this:
with open('test.jpg', 'rb') as payload: files = { 'photo': payload, 'Content-Type': 'image/jpeg' } r = requests.put( 'http://localhost:8000/sendfiles/1/', verify=False, files=files ) print(r)Requests will see that "files" parameter has content and will set the headers accordingly.
更多推荐
发布评论