N
N
NyxDeveloper2021-02-25 10:08:53
Django
NyxDeveloper, 2021-02-25 10:08:53

How to give image url to drf as link to image?

I have already asked this question before, but apparently either I myself understand everything very poorly, or I don’t ask at all like that. You need to give the image in the / media folder so that it can be inserted as <img src="https://media/my_img.jpeg">in html on the client side. The Django Rest Framework has the ability to create serialization classes, but they require additional logic to be written in views.py. Is there a way to specify the path to the image from the file system inside the json object without adding unnecessary logic to views?
Thank you!
UPD:
json is returned like this:

return JsonResponse({
                'id': menu.id,
                'name': menu.name,
                'img': os.path.join(MEDIA_ROOT, menu.img.url)
           })

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2021-02-25
@NyxDeveloper

https://docs.djangoproject.com/en/3.1/ref/request-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question