首页 \ 问答 \ 无法将图像上传到django(cannot upload image to django)

无法将图像上传到django(cannot upload image to django)

我需要用django rest框架将图像上传到django服务器。 我试图使用httpie发布图像,我得到这个错误。

http 400没有图像被提交。

serializers.py

from rest_framework import serializers

from myapp.models import *


class PhotoSerializer(serializers.ModelSerializer):


    image = serializers.ImageField(max_length=None,use_url=True)


    class Meta:
        model = MyPhoto
        fields = ('id', 'image')

models.py

from django.db import models

class MyPhoto(models.Model):

    image = models.ImageField(upload_to='photos/', max_length=254)

views.py

from rest_framework.views import APIView
from myapp.models import *
from myapp.serializers import PhotoSerializer
from rest_framework import status
from rest_framework.response import Response
from rest_framework.parsers import FormParser, MultiPartParser


class PhotoList(APIView):

    parser_classes = (FormParser, MultiPartParser)

    def get(self, request, format=None):

        photo = MyPhoto.objects.all()

        serializer = PhotoSerializer(photo, many=True)
        return Response(data=serializer.data, status=status.HTTP_200_OK)

    def post(self, request, format=None):

       serializer = PhotoSerializer(data=request.data)
       if serializer.is_valid():
           serializer.save()
           return Response(serializer.data, status=status.HTTP_201_CREATED)
       return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

我设置了MEDIA_ROOTMEDIA_URL


I need to upload an image to django server with django rest framework. I tried to post the image using httpie and I am getting this error.

http 400 no image was submitted.

serializers.py

from rest_framework import serializers

from myapp.models import *


class PhotoSerializer(serializers.ModelSerializer):


    image = serializers.ImageField(max_length=None,use_url=True)


    class Meta:
        model = MyPhoto
        fields = ('id', 'image')

models.py

from django.db import models

class MyPhoto(models.Model):

    image = models.ImageField(upload_to='photos/', max_length=254)

views.py

from rest_framework.views import APIView
from myapp.models import *
from myapp.serializers import PhotoSerializer
from rest_framework import status
from rest_framework.response import Response
from rest_framework.parsers import FormParser, MultiPartParser


class PhotoList(APIView):

    parser_classes = (FormParser, MultiPartParser)

    def get(self, request, format=None):

        photo = MyPhoto.objects.all()

        serializer = PhotoSerializer(photo, many=True)
        return Response(data=serializer.data, status=status.HTTP_200_OK)

    def post(self, request, format=None):

       serializer = PhotoSerializer(data=request.data)
       if serializer.is_valid():
           serializer.save()
           return Response(serializer.data, status=status.HTTP_201_CREATED)
       return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

I have setMEDIA_ROOT and MEDIA_URL.


原文:https://stackoverflow.com/questions/42442348
更新时间:2024-04-23 07:04

最满意答案

如果我需要解决这个问题,我会使用某种“差异”系统。 我不知道PHP是否存在,但我确信它确实如此。

如果你有办法区分邮件,那么更具技术性。 我会把没有标题的原始邮件(让我们称之为邮件A),以及没有标题的新邮件(邮件B)。 然后就像比较邮件A和B一样容易,看看有什么区别。 添加的行是用户的响应。

有些事你需要自己寻找,因为我现在还不知道答案:

  • 如果用户没有发送原始邮件怎么办? 例如,您可以仅使用从差异中添加的行,而不是那些已删除的行。
  • 如果用户在您的回复中更改了内联内容,该怎么办? 我不知道如何解决这个问题。

我的解决方案实际上是不给用户那么大的自由。 只需准备一个用户应该写信息的地方。 类似于:“在以下行<line> <line>”之间写下您的消息,或者“<line>在上一行之上写下您的消息”。 然后你知道在哪里寻找文本。

你给予的自由越多,对你来说就越难。 您可以从一个简单的系统开始,逐步提高系统的智能。 这就是我大部分时间的开始:从简单的事情开始,逐渐增加难度。


If I would need to solve this question, I would be using some kind of 'diff' system. I don't' know if that exists for PHP, but I'm confident it does.

So then more technical, if you have a way to diff the mails. I would take the original mail without the header (let's call it mail A), and also the new mail without the header (mail B). Then it's as easy as comparing mail A with B and see what the difference is. The lines that were added, are the response of the user.

Some things you will have to look for yourself, because I don't know an answer for them at this moment:

  • What if the user doesn't send the original message? You could for example only use the lines that were added from your diff, not those which were deleted.
  • What if the user changes something inline in your response? I have no idea how to solve this.

My solution would actually be to don't give the users that much freedom. Just prepare a place where the users should write their message. Something like: "Write your message between the following lines <line> <line>", or "<line>Write your message above the previous line". Then you know where to look for the text.

The more freedom you give, the more difficult it becomes for you. You can start with an easy system, and gradually improve the intelligence of the system. That's how I start most of the time: start from something simple and increase the difficulty gradually.

相关问答

更多
  • 如果你改变会发生什么: $headers .= "From: ".$_POST["fieldFormName"]."<".$_POST["fieldFormus"].">\nReply-To: ".$_POST["fieldFormEmail"].""; 至 $headers .= "From: ".$_POST["fieldFormName"]."<".$_POST["fieldFormus"].">\nReply-To: ".$_POST["fieldFormEmail"]."\n"; 注意行末尾的 ...
  • @Mahmood Rehman你实现这篇文章: http ://davidwalsh.name/gmail-php-imap ??? 我希望下一行可以帮到你。 你觉得使用是个好主意:PHP DOM( http://php.net/manual/en/book.dom.php ) 更具体的DOMDocument :: loadHTML AND DOMElement :: getElementsByTagName。 @Mahmood Rehman you implement this Article : htt ...
  • 在第三方电子邮件的情况下,在HTML电子邮件开发的常规过程要设置所有样式内联时,已将