简体   繁体   中英

django how to access an already uploaded video

i have uploaded a video to django and it's all working fine .. but i need to render that video in the view.. django cant access it
any idea how to make django template see my uploaded files

modles.py :

class VideoData(models.Model):

    title = models.CharField(max_length=200)
    file = models.FileField(upload_to='vids'  )

forms.py :

class UploadFileForm(forms.Form):
    title = forms.CharField(max_length=50)
    file  = forms.FileField()

and in template.html:

<video width="320" height="240" controls="controls">
  <source src="vids/name.mov"  type="video/mov" />
 </video>

it shows an empty video player.

am i doing anything wrong ?

please help

Your video is uploaded media_root/vid directory(just check it on file system to make sure). so when you call it from template you should write your media template in front of your video image. (So your url could be /media/vids/name.mov) I also noticed that you are writing hardcoded video name here use something like following instead

<video width="320" height="240" controls="controls">
  <source src="{{MEDIA_URL}}{{my_video_data.file}}"  type="video/mov" />
 </video>

in order to use MEDIA_URL here you have to run your context variable through context processors checkout django.shortcuts.render method.

Assuming an instance of VideoData named vid is in the template's context, you should use:

<video width="320" height="240" controls="controls">
  <source src="{{vid.file.url}}"  type="video/mov" />
 </video>

To add the VideoData instance to the template's context, your view should look like this:

from django.shortcuts import render_to_response
from models import VideoData

def show_video(request):
    vid = VideoData.objects.get(id=1) # replace with correct query
    return render_to_response("template.html", {"vid":vid})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM