Django - Displaying video with jwplayer

2019-07-25 11:29发布

问题:

I am currently trying to display a video on a website using jwplayer. The view for the page is

def video(request):
        return render_to_response('video_player/video.html', context_instance=RequestContext(request)

And the html template being used contains this in the head:

<script type="text/javascript" src="/jwplayer/jwplayer.js"></script>

And this in the body:

<div id="myElement">Loading the player...</div>
<script type="text/javascript">
    jwplayer("myElement").setup({
        file: "{{ MEDIA }}videos/test.mp4",
        image: "{{ MEDIA }}videos/cute-bunny.jpg"
    });
</script>

It doesn't display anything other than 'Loading the player', i think there may be something wrong with me calling the media_root. It is defined as:

MEDIA_ROOT = 'C:/Users/Timmy/Documents/GitHub/GroupProject/media'

回答1:

You should be using the {{ MEDIA_URL }} tag in your templates, which you define in settings.py.

Example in settings.py:

MEDIA_URL = '/media/'

MEDIA_ROOT, like STATIC_ROOT, is the directory that Django uses to upload media files to and serve media files from, not the URL path.

See: https://docs.djangoproject.com/en/dev/howto/static-files/#serving-files-uploaded-by-a-user