python - 在 Django 模板中显示视频:媒体链接
问题描述
当前尝试从模型中提取视频,但似乎无法通过将媒体目录添加到提取的 URL 的前面来找到正确的 url。
我是否正确地从模型中提取了 url?
Settings.py(媒体部分)
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
urls.py(在应用程序中)
from django.urls import path
from . import views
urlpatterns = [
path('', views.video_form_upload, name='highlights'),
path('outputs', views.video_output, name='output')
]
更新
video_output.html
{% extends 'highlights/base.html' %}
{% block content %}
{% for video in highlights %}
<video width="320" height="240" controls>
{% load static %}
<source src="{% static "{{ video.highlight.url }}" %}" type="video/mp4"></source>
Your browser does not support the video tag
</video>
{% endfor %}
{% endblock %}
示例输出视频 URL
<video width="320" height="240" controls>
<source src="/media/%7B%7B%20video.highlight.url%20%7D%7D" type="video/mp4"></source>
Your browser does not support the video tag
</video>
解决方案
您需要将媒体 URL 添加到开发中的 urlpatterns。
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
推荐阅读
- java - bc-fips 库非法反射访问警告
- sql - 如何在 Hive 中查询最后 8 周的数据
- python - 如何在另一台服务器上的另一个 virtualenv 环境中运行 django 文件?
- html - 在上述列具有偏移量的 boostrap 行下方对齐内容
- javascript - 支付成功后如何调用函数?在角
- ios - NotificationSendingSplitViewcontroller,它是干什么用的?
- python - 优化此功能--numpy广播问题
- php - 我想在 php 中回显所有具有相同用户 ID 的类名
- docusignapi - 嵌入式签名欧盟合格 IDnow TSP:更改签名者的电子邮件地址而无需重新识别?
- java - 使用 Spring MVC 无法同时正确打印 ArrayList 和 HTML 中的表单