Html 使用 Django 和 html5 标签播放视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20953301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:21:36  来源:igfitidea点击:

Playing Video with Django and html5 tag

djangohtmlvideohtml5-video

提问by SamuraiT

I am trying to play a video by using django with html5 video tag but couldn't.
The main problem is that the server cannot get a videofile.
I got this error:

我正在尝试使用带有 html5 视频标签的 django 播放视频,但不能。
主要问题是服务器无法获取video文件。
我收到此错误:

[06/Jan/2014 23:52:07] "GET absolute_path_of_media/sample.mp4 HTTP/1.1" 404 2422

and in inspect elements:

并在检查元素中:

get error

得到错误

Here, I will show you my code.
templates/videoplay.html:

在这里,我将向您展示我的代码。
模板/videoplay.html:

{% extends "app/base.html" %}
{% block contents %}
<video name='demo' controls autoplay width='50%' height='40%'>
<source src="{{media}}/sample.mp4" type="video/mp4"></source>
</video>
{% endblock %}

views.py:

视图.py:

def index(request):
    return render(request, "app/videoplay.html", {'media': MEDIA_ROOT}) 

I imported MEDIA_ROOTfrom settings.py and it is absolute path of media directory.

MEDIA_ROOT从 settings.py导入,它是媒体目录的绝对路径。

develop environment:

开发环境:

browser: chrome 
django:1.6.1
python:2.7

static and media directories' relation:

静态和媒体目录的关系:

mysite/
      static/
            sample.mp4
      media/
            sample.mp4
      templates/
            ....
      views.py
      ....

回答by ptr

You're missing the slash at the start of the file URL, and you don't need to pass the MEDIA_ROOT into the context, use the {{ STATIC_URL }} template variable that you set in your settings file.

您缺少文件 URL 开头的斜杠,并且不需要将 MEDIA_ROOT 传递到上下文中,请使用您在设置文件中设置的 {{ STATIC_URL }} 模板变量。

To clarify from the comments below, your MEDIA_ROOT setting is where django will store media files (user uploaded). The STATIC_ROOT is where django will look for static files (your js/css/images etc).

为了从下面的评论中澄清,您的 MEDIA_ROOT 设置是 django 将存储媒体文件(用户上传)的地方。STATIC_ROOT 是 django 查找静态文件(你的 js/css/images 等)的地方。

The STATIC_URLand MEDIA_URLsettings are used in the template. They will be set to something like STATIC_URL = "/static/"and MEDIA_URL = "/media/"and they are passed to the template by django so when you do:

STATIC_URLMEDIA_URL设置可以在模板中使用。它们将被设置为类似STATIC_URL = "/static/"andMEDIA_URL = "/media/"并且它们被 django 传递给模板,所以当你这样做时:

<img src="{{ STATIC_URL }}sample.jpg" />

or

或者

<source src="{{ MEDIA_URL }}sample.mp4" type="video/mp4"></source>

it replaces {{ STATIC_URL}}with "/static/"so you end up with "/static/sample.jpg"as the src url which django uses to fetch your file from the STATIC_ROOT.

它替换{{ STATIC_URL}}"/static/"所以你最终"/static/sample.jpg"得到 src url,django 使用它从 STATIC_ROOT 获取你的文件。

回答by Ranvijay Sachan

you can try this :

你可以试试这个:

{% load staticfiles %}
<!DOCTYPE html>
<html>
<body>

<video width="530" height="440" controls autoplay>
  <source src="{% static "mov_bbb.mp4" %}" type="video/mp4"> </source>
</video>
</body>
</html>

setting.py

设置.py

STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"), )

view.py

查看.py

from django.views.generic.base import TemplateView


class HomeView(TemplateView):

    template_name = 'home.html'

urls.py

网址.py

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.HomeView.as_view(), name='home'),
]

static file:

静态文件:

static/
            mov_bbb.mp4

回答by Daniel Roseman

You don't want the absolute path to your directory. You want the URL that the media is actually being served on, which might be something like "/static/sample.mp4"(or however you've configured STATIC_URL.

您不想要目录的绝对路径。您需要实际提供媒体的 URL,这可能类似于"/static/sample.mp4"(或者您已经配置了STATIC_URL.