I am building a Django application that stores image files in a mongodb GridFS.
I use Djongo to work with the database and followed this example https://www.djongomapper.com/using-django-with-mongodb-gridfs/ to store the images to the DB.
now I can, currently through the admin page, upload images to the DB, which need to be accessed using a URL like this:
http://127.0.0.1:8000/files/60fae4884db41b9ad761c8b0
Now I have this in the urls.py
urlpatterns = [
...
path('files/<str:fileid>', views.files, name='files'),
]
But in View file I don't know how to retrieve the image from the DB:
@login_required
def files(request, fileid):
return response
I searched the documentation of Djongo and Django but couldn't find an easy way to do it.
Note: In the main DB collection only the image file name is stored. In gridfs collection 'files' an ID (the one in the URL), the image name (the only link to the main collection) and other details are stored. And in the 'chunks' collection there is an ID, a files_ID (foreign key to the files ID) and the binary data.