File Upload using Django Rest Framework getting error “No file was submitted.”











up vote
0
down vote

favorite












While trying to upload the file using Postman with Content-Type - multipart/form-data in the headers. I am passing both the fields, but I get the following error:




Error:{"upload_to": ["This field is required."],"file_object": ["No
file was submitted."]}




urls.py



from django.conf.urls import include, url
from rest_framework_nested import routers
from utils.views import TemporaryImageView
from . import views

router = routers.SimpleRouter(trailing_slash=False)

urlpatterns = [
url(r'^', include(router.urls)),
url(r'^upload-temp-image/$', TemporaryImageView.as_view())
]


views.py



from rest_framework import viewsets, filters
import django_filters.rest_framework
from rest_framework.generics import CreateAPIView
from rest_framework.parsers import FileUploadParser, MultiPartParser, FormParser
from utils.serializers.temporary_image import TemporaryImageSerializer

class TemporaryImageView(CreateAPIView):
parser_classes = (MultiPartParser,)
serializer_class = TemporaryImageSerializer


serializers.py



from rest_framework import serializers
from utils.models.tempfile import TemporaryFile

class TemporaryImageSerializer(serializers.ModelSerializer):
choices = (('Company Logo','/company/logos/'),
)
upload_to = serializers.ChoiceField(choices=choices)
file_object = serializers.ImageField()

class Meta:
model = TemporaryFile
fields = ('upload_to', 'file_object')


models.py



from django.db import models
class TemporaryFile(models.Model):
"""
a temporary file to backend
"""

file_object = models.FileField(blank=False, null=False)
timestamp = models.DateTimeField(auto_now_add=True)

def __unicode__(self):
return str(self.id)


Please help...I don't know what is wrong.










share|improve this question
























  • Did you remember to include enctype="multipart/form-data" in your template where the form is submitted?
    – robotHamster
    Nov 20 at 7:15










  • yes..i am providing
    – Stephy Joseph
    Nov 20 at 8:29















up vote
0
down vote

favorite












While trying to upload the file using Postman with Content-Type - multipart/form-data in the headers. I am passing both the fields, but I get the following error:




Error:{"upload_to": ["This field is required."],"file_object": ["No
file was submitted."]}




urls.py



from django.conf.urls import include, url
from rest_framework_nested import routers
from utils.views import TemporaryImageView
from . import views

router = routers.SimpleRouter(trailing_slash=False)

urlpatterns = [
url(r'^', include(router.urls)),
url(r'^upload-temp-image/$', TemporaryImageView.as_view())
]


views.py



from rest_framework import viewsets, filters
import django_filters.rest_framework
from rest_framework.generics import CreateAPIView
from rest_framework.parsers import FileUploadParser, MultiPartParser, FormParser
from utils.serializers.temporary_image import TemporaryImageSerializer

class TemporaryImageView(CreateAPIView):
parser_classes = (MultiPartParser,)
serializer_class = TemporaryImageSerializer


serializers.py



from rest_framework import serializers
from utils.models.tempfile import TemporaryFile

class TemporaryImageSerializer(serializers.ModelSerializer):
choices = (('Company Logo','/company/logos/'),
)
upload_to = serializers.ChoiceField(choices=choices)
file_object = serializers.ImageField()

class Meta:
model = TemporaryFile
fields = ('upload_to', 'file_object')


models.py



from django.db import models
class TemporaryFile(models.Model):
"""
a temporary file to backend
"""

file_object = models.FileField(blank=False, null=False)
timestamp = models.DateTimeField(auto_now_add=True)

def __unicode__(self):
return str(self.id)


Please help...I don't know what is wrong.










share|improve this question
























  • Did you remember to include enctype="multipart/form-data" in your template where the form is submitted?
    – robotHamster
    Nov 20 at 7:15










  • yes..i am providing
    – Stephy Joseph
    Nov 20 at 8:29













up vote
0
down vote

favorite









up vote
0
down vote

favorite











While trying to upload the file using Postman with Content-Type - multipart/form-data in the headers. I am passing both the fields, but I get the following error:




Error:{"upload_to": ["This field is required."],"file_object": ["No
file was submitted."]}




urls.py



from django.conf.urls import include, url
from rest_framework_nested import routers
from utils.views import TemporaryImageView
from . import views

router = routers.SimpleRouter(trailing_slash=False)

urlpatterns = [
url(r'^', include(router.urls)),
url(r'^upload-temp-image/$', TemporaryImageView.as_view())
]


views.py



from rest_framework import viewsets, filters
import django_filters.rest_framework
from rest_framework.generics import CreateAPIView
from rest_framework.parsers import FileUploadParser, MultiPartParser, FormParser
from utils.serializers.temporary_image import TemporaryImageSerializer

class TemporaryImageView(CreateAPIView):
parser_classes = (MultiPartParser,)
serializer_class = TemporaryImageSerializer


serializers.py



from rest_framework import serializers
from utils.models.tempfile import TemporaryFile

class TemporaryImageSerializer(serializers.ModelSerializer):
choices = (('Company Logo','/company/logos/'),
)
upload_to = serializers.ChoiceField(choices=choices)
file_object = serializers.ImageField()

class Meta:
model = TemporaryFile
fields = ('upload_to', 'file_object')


models.py



from django.db import models
class TemporaryFile(models.Model):
"""
a temporary file to backend
"""

file_object = models.FileField(blank=False, null=False)
timestamp = models.DateTimeField(auto_now_add=True)

def __unicode__(self):
return str(self.id)


Please help...I don't know what is wrong.










share|improve this question















While trying to upload the file using Postman with Content-Type - multipart/form-data in the headers. I am passing both the fields, but I get the following error:




Error:{"upload_to": ["This field is required."],"file_object": ["No
file was submitted."]}




urls.py



from django.conf.urls import include, url
from rest_framework_nested import routers
from utils.views import TemporaryImageView
from . import views

router = routers.SimpleRouter(trailing_slash=False)

urlpatterns = [
url(r'^', include(router.urls)),
url(r'^upload-temp-image/$', TemporaryImageView.as_view())
]


views.py



from rest_framework import viewsets, filters
import django_filters.rest_framework
from rest_framework.generics import CreateAPIView
from rest_framework.parsers import FileUploadParser, MultiPartParser, FormParser
from utils.serializers.temporary_image import TemporaryImageSerializer

class TemporaryImageView(CreateAPIView):
parser_classes = (MultiPartParser,)
serializer_class = TemporaryImageSerializer


serializers.py



from rest_framework import serializers
from utils.models.tempfile import TemporaryFile

class TemporaryImageSerializer(serializers.ModelSerializer):
choices = (('Company Logo','/company/logos/'),
)
upload_to = serializers.ChoiceField(choices=choices)
file_object = serializers.ImageField()

class Meta:
model = TemporaryFile
fields = ('upload_to', 'file_object')


models.py



from django.db import models
class TemporaryFile(models.Model):
"""
a temporary file to backend
"""

file_object = models.FileField(blank=False, null=False)
timestamp = models.DateTimeField(auto_now_add=True)

def __unicode__(self):
return str(self.id)


Please help...I don't know what is wrong.







django django-file-upload






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 11:18









albert

2,71221021




2,71221021










asked Nov 20 at 5:48









Stephy Joseph

206




206












  • Did you remember to include enctype="multipart/form-data" in your template where the form is submitted?
    – robotHamster
    Nov 20 at 7:15










  • yes..i am providing
    – Stephy Joseph
    Nov 20 at 8:29


















  • Did you remember to include enctype="multipart/form-data" in your template where the form is submitted?
    – robotHamster
    Nov 20 at 7:15










  • yes..i am providing
    – Stephy Joseph
    Nov 20 at 8:29
















Did you remember to include enctype="multipart/form-data" in your template where the form is submitted?
– robotHamster
Nov 20 at 7:15




Did you remember to include enctype="multipart/form-data" in your template where the form is submitted?
– robotHamster
Nov 20 at 7:15












yes..i am providing
– Stephy Joseph
Nov 20 at 8:29




yes..i am providing
– Stephy Joseph
Nov 20 at 8:29












1 Answer
1






active

oldest

votes

















up vote
0
down vote













I have changed my models.py file in the way below and it works as expected..



models.py



from django.db import models



def get_image_path(instance, filename):



if instance.upload_to == "company_logo":
path = 'company/logo/'
return path


class TemporaryFile(models.Model):
"""
a temporary file to backend
"""



file_object = models.FileField(blank=False, null=False, upload_to=get_image_path)
timestamp = models.DateTimeField(auto_now_add=True)

def __unicode__(self):
return str(self.id)

def __init__(self, *args, **kwargs):
self.upload_to = kwargs.pop("upload_to")
super(TemporaryFile, self).__init__(*args, **kwargs)





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386945%2ffile-upload-using-django-rest-framework-getting-error-no-file-was-submitted%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    I have changed my models.py file in the way below and it works as expected..



    models.py



    from django.db import models



    def get_image_path(instance, filename):



    if instance.upload_to == "company_logo":
    path = 'company/logo/'
    return path


    class TemporaryFile(models.Model):
    """
    a temporary file to backend
    """



    file_object = models.FileField(blank=False, null=False, upload_to=get_image_path)
    timestamp = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
    return str(self.id)

    def __init__(self, *args, **kwargs):
    self.upload_to = kwargs.pop("upload_to")
    super(TemporaryFile, self).__init__(*args, **kwargs)





    share|improve this answer

























      up vote
      0
      down vote













      I have changed my models.py file in the way below and it works as expected..



      models.py



      from django.db import models



      def get_image_path(instance, filename):



      if instance.upload_to == "company_logo":
      path = 'company/logo/'
      return path


      class TemporaryFile(models.Model):
      """
      a temporary file to backend
      """



      file_object = models.FileField(blank=False, null=False, upload_to=get_image_path)
      timestamp = models.DateTimeField(auto_now_add=True)

      def __unicode__(self):
      return str(self.id)

      def __init__(self, *args, **kwargs):
      self.upload_to = kwargs.pop("upload_to")
      super(TemporaryFile, self).__init__(*args, **kwargs)





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        I have changed my models.py file in the way below and it works as expected..



        models.py



        from django.db import models



        def get_image_path(instance, filename):



        if instance.upload_to == "company_logo":
        path = 'company/logo/'
        return path


        class TemporaryFile(models.Model):
        """
        a temporary file to backend
        """



        file_object = models.FileField(blank=False, null=False, upload_to=get_image_path)
        timestamp = models.DateTimeField(auto_now_add=True)

        def __unicode__(self):
        return str(self.id)

        def __init__(self, *args, **kwargs):
        self.upload_to = kwargs.pop("upload_to")
        super(TemporaryFile, self).__init__(*args, **kwargs)





        share|improve this answer












        I have changed my models.py file in the way below and it works as expected..



        models.py



        from django.db import models



        def get_image_path(instance, filename):



        if instance.upload_to == "company_logo":
        path = 'company/logo/'
        return path


        class TemporaryFile(models.Model):
        """
        a temporary file to backend
        """



        file_object = models.FileField(blank=False, null=False, upload_to=get_image_path)
        timestamp = models.DateTimeField(auto_now_add=True)

        def __unicode__(self):
        return str(self.id)

        def __init__(self, *args, **kwargs):
        self.upload_to = kwargs.pop("upload_to")
        super(TemporaryFile, self).__init__(*args, **kwargs)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 28 at 5:41









        Stephy Joseph

        206




        206






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386945%2ffile-upload-using-django-rest-framework-getting-error-no-file-was-submitted%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Tonle Sap (See)

            I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

            Guatemaltekische Davis-Cup-Mannschaft