从Python脚本上传图片到Django(Upload picture to Django from Python script)

编程入门 行业动态 更新时间:2024-10-26 20:30:56
从Python脚本上传图片到Django(Upload picture to Django from Python script)

我正在尝试从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.


更多推荐

本文发布于:2023-08-05 21:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1439095.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上传图片   脚本   Python   Django   script

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!