37๐
In django REST framework. we have components like Parsers, Renderers and Serializers.
-
The responsibility of Parsers is to parse the data that is sent by request methods GET, POST and PUT, etc.
-
Default parser used in django REST is โJSONParserโ. It only parses the data JSON data[numbers, string, date]. It ignores the data like FILES.
-
In order to parse the FILES we need to use parsers like โMultiPartParserโ or โFormParserโ.
Example Code :
from rest_framework.parsers import MultiPartParser from rest_framework.response import Response from rest_framework.views import APIView class ExampleView(APIView): """ A view that can accept POST requests with JSON content. """ parser_classes = (MultiPartParser,) def post(self, request, format=None): # to access files print request.FILES # to access data print request.data return Response({'received data': request.data})
When we use property request.data
then parser will parse the data.
References: Django REST Docs, Django REST Github
4๐
I face the same problem.The problem error message shows:
{โdetailโ:โMissing filename. Request should include a
Content-Disposition header with a filename parameter.โ}
I do all the steps above my answer but it doesnโt work.Finally,
I find the reason is in the backend of viewset.
it shows like this
parser_classes = (FileUploadParser, MultiPartParser, FormParser)
and I remove the FileUploadParser
parser_classes = ( MultiPartParser, FormParser)
and It works, so I think you should pay more attention to it
- [Django]-Passing STATIC_URL to file javascript with django
- [Django]-Suddenly when running tests I get "TypeError: 'NoneType' object is not iterable
- [Django]-Django-nonrel + Django-registration problem: unexpected keyword argument 'uidb36' when resetting password
3๐
Adding
Content-Disposition: attachment; filename=<insert-your-file-name>
to the header has solved my issue, and then in the view method where you access this file in the following manner:
file = self.request.FILES['file']
- [Django]-Django Admin app or roll my own?
- [Django]-Substring in a django template?
- [Django]-Django Footer and header on each page with {% extends }
2๐
On the second error Missing filename. Request should include a Content-Disposition header with a filename parameter.
using postman I removed the header Content-Type :multipart/form-data
and was successful.
It seems the issue is the custom Content-Type header overrides the default Content-Type header that should be sent. Check this thread for reference Content-Type for multipart posts
- [Django]-What does 'many = True' do in Django Rest FrameWork?
- [Django]-How do I install psycopg2 for Python 3.x?
- [Django]-Django models: mutual references between two classes and impossibility to use forward declaration in python
2๐
You donโt need to use MultipartParser
or FormParser
.
What you need is a serializer with a FileField()
like so:
serializers.py:
class FileUploadSerializer(serializers.Serializer):
# I set use_url to False so I don't need to pass file
# through the url itself - defaults to True if you need it
file = serializers.FileField(use_url=False)
So then when you try to access file
here below, youโll have a dict with a key named file
. Personally, Iโd probably name it something more descriptive than just โfileโ, but thatโs up to you.
from .serializers import FileUploadSerializer
class FileUploadView(APIView):
def post(self, request):
# set 'data' so that you can use 'is_vaid()' and raise exception
# if the file fails validation
serializer = FileUploadSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
# once validated, grab the file from the request itself
file = request.FILES['file']
- [Django]-Allowing RabbitMQ-Server Connections
- [Django]-Django Admin app or roll my own?
- [Django]-Django-nonrel + Django-registration problem: unexpected keyword argument 'uidb36' when resetting password
0๐
in your views.py change the parser like this
parser_classes = (JSONParser, MultiPartParser)
- [Django]-Name '_' is not defined
- [Django]-Django Multiple Authentication Backend for one project
- [Django]-Django composite unique on multiple model fields