javascript – How do I pass the path to an image in AngularJS for Django REST?

Question:

How to pass to a function or get the full path to a picture inside a function? In the end, I need to pass the path to this picture to the server so that Django REST can find this file in the file system there.

<img ng-src="/media/{{ photo }}" />

where photo is the name of the image file

As a result, the path to the picture is converted to http://имя_сайта/media/название_файла

Here, in another tag, the function myfunc(photo) i.e. as an input, it receives the value of the image file name.

The real path to the media directory is registered in the MEDIA_ROOT variable in settings.py . If, suppose, I pass in REST the path to the picture in the form 'имя_файла' , then it remains to get the value of the MEDIA_ROOT variable inside the API, how can I do this?

If I understand correctly, in the REST API you need to get os.path.join(settings.MEDIA_ROOT, request.data['image_url']) , where image_url = имя_файла

Answer:

In order to transfer the path to the image from AngularJS to Django REST , you need to send the name of the image from AngularJS via an http request to the API on the server side, and take os.path.join(settings.MEDIA_ROOT, request.data['image_url']) where image_url – file name, settings.MEDIA_ROOT – contains the value of MEDIA_ROOT of the configuration file settings.py for Django.

Scroll to Top