django在视图功能中获取媒体网址

时间:2021-03-06 18:20:47

Serve Media File in view function

在视图功能中提供媒体文件

View.py

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

When i perform above function, the fullpath is throwing below error :

当我执行上述功能时,fullpath将抛出以下错误:

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

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

但实际文件存在于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")

这是我的媒体根MEDIA_ROOT = os.path.join(PACKAGE_ROOT,“site_media”,“media”)

What should i do for getting media url in view function ?

如何在视图功能中获取媒体网址?

1 个解决方案

#1


11  

You can access to your settings variables by:

您可以通过以下方式访问设置变量:

from django.conf import settings

your_media_root = settings.MEDIA_ROOT

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

但您也可以像获取名称一样访问文件路径:

name = file_path.file.name
url = file_path.file.url
path = file_path.file.path

#1


11  

You can access to your settings variables by:

您可以通过以下方式访问设置变量:

from django.conf import settings

your_media_root = settings.MEDIA_ROOT

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

但您也可以像获取名称一样访问文件路径:

name = file_path.file.name
url = file_path.file.url
path = file_path.file.path