![]() NB: The request might take a few seconds to run depending on your internet connection Writing automated tests for the endpoint Setting up the test and installing pytest-django Then, we send the request to our API and get the expected result. In the above screenshots we see that we are using form-data to select images from our local machine. The screenshots below show a sample of how the endpoint works: We can test our code using postman to see that it working. Path('api/upload-image', UploadView.as_view()), We do this by updating urls.py as follows from upload_app.views import UploadView Now it is time to add the upload url to our app. Then, the post method tells tries to get a file in picture attribute of the request body, uploads the image to cloudinary using and finally sends the response from cloudinary back to the user. The MultiPartParser allows the View to be able to recognise images when they are sent to the application. The UploadView class specifies a set of parsers. The first few lines of the file import the necessary modules and creates UploadView class that is an APIView. ![]() You can implement the endpoint by adding the view in upload_ to from rest_framework.views import APIViewįrom rest_framework.response import Responseįrom rest_framework.parsers import MultiPartParser, JSONParser Note that ideally, you would want to put config data like these in an environmental variable as you don't want these sensitive info to be exposed to the public Implement the endpoint ![]() In order to configure Cloudinary, go to settings.py and add the following block of code at the top. Once this is done you would be able to obtain an API_KEY, API_SECRET and CLOUD_NAME. If everything works well you should be able to start the server by running: python manage.py runserverīefore we can create start to upload images to cloudinary, we must first create a cloudinary account at the Cloudinary Website. Next thing is to add Django Rest Framework and upload_app in the list of installed apps by modifying the INSTALLED_APPS array in upload_project/settings.py. The folder structure should look like this. Now we create a new django app, upload_app by running: python manage.py startapp upload_app This would create a project and our working directory would look like this. Once our dependencies have been installed, we then create our Django project by running: django-admin startproject upload_project. NB: If you have worked with django and django-rest-framework before you should be able to skip this section pipenv install django djangorestframework cloudinary Pipenv shell # creates virtual environment using pipenv ![]() Next thing is to setup a virtual environment using pipenv as follows: sudo pip install pipenv #installs pipenv The above commands would create a directory called django-app and enter that directory from your terminal Let's start by creating a working directory which we would call django-app. Setup Virtual environment and install dependencies The whole process was pretty straight forward but things got a little interesting when I tried to test the endpoint.Īfter solving this problem, I thought it wise to write a blog post that kinda explains all that I did from the setup to the testing. I recently implemented an image upload endpoint in Cloudinary, using python and Django Rest Framework.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |