Django get media url in view function

Serve media in view mode

View.py

file_path = Tracks.objects.get(pk=event_id)
name = file_path.file.name
fullpath = os.path.abspath(name)

When I perform the above function, the full path is thrown below the error:

[Errno 2] No such file or directory: '/home/ri/studio/videotube/uploads/2014/10/15/Wildlife_512kb_hVnnOc2.mp4'

But the actual file is in file:///home/ri/studio/videotube/videotube/site_media/media/uploads/2014/10/15/Wildlife_512kb_hVnnOc2.mp4

This is my media root MEDIA_ROOT = os.path.join(PACKAGE_ROOT, "site_media", "media")

What should I do to get media url in view function?

+4
source share
1 answer

You can access your parameter variables:

from django.conf import settings

your_media_root = settings.MEDIA_ROOT

But you can also access the file path the same way you get the name:

name = file_path.file.name
url = file_path.file.url
path = file_path.file.path
+8
source

All Articles