3

I am not able to play video on my Django website. I think something is wrong in my views.py render function

videoplay.html:

{% block contents %} <video name='demo' controls autoplay width='50%' height='40%'> <source src="{{STATIC_URL}}sample1.mp4" type="video/mp4"></source> </video> {% endblock %} 

Included this in settings.py:

 STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media')` 

Views.py is:

 from django.shortcuts import render, redirect from django.conf import settings from django.core.files.storage import FileSystemStorage from django.views.generic.base import TemplateView from uploads.core.models import Document from uploads.core.forms import DocumentForm def index(request): return render(request, "core/videoplay.html")` 

I am getting a play button and streaming line on my website

4
  • What does the dev console look like. I am guessing it will be that the video source is not loading because I'm not sure that you are loading the static file correctly. I'd probably do {% load static %} <img src="{% static "my_app/example.jpg" %}" alt="My image"/> docs.djangoproject.com/en/1.11/howto/static-files Commented Nov 16, 2017 at 0:12
  • Actually I am handling video file and not image file that's why it won't work.I have already tried this .I am sure something needs to be added in my render request. Commented Nov 16, 2017 at 0:17
  • Check the rendered HTML to see if the src contains correct URL. I too would guess there's something wrong with your STATIC tag and directory setup. Commented Nov 16, 2017 at 0:21
  • I just replaced my source tag with this <source src="{{MEDIA_URL}}sample1.mp4" type="video/mp4"></source> and it is working now Commented Nov 16, 2017 at 0:44

1 Answer 1

4

The STATIC_URL is where your static files are located such as CSS, Template Images, Javascript etc. There is a special Template Tag for Static Files in Django called {% static %}.

To generate the full url to where the static files are located, you use the template tag like this:

{% load static %} <video src="{% static "/videos/demo.mp4" %}"> 

The MEDIA_URL is where your media files are located. Media files are uploaded files that change during the applications lifetime. They are not necessarily there at deployment but can be uploaded at any time by users or admins on the website.

Media files are set as fields on the model like this:

class DemoVideo(models.Model): video = models.FileField(upload_to="videos") 

They are accessed like this demovideo.video.url so in your template it would be src="{{demovideo.video.url}}.

Check if your file is a media or static file, and access it in the template in the correct way described above.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.