视频上传并显示在Django网站上

时间:2019-05-09 10:18:28

标签: django django-models django-templates django-views

我有一个上传视频的模型,我想在浏览器中显示它,但是以某种方式无法显示。请帮助我。

我制作了一个名为deploy的应用程序,我在其中上传视频并将其保存。请告诉我我在哪里做错了,应该怎么做。我希望已上传的视频应显示在页面上,并且还应该有一个下载选项。我将非常感谢您的帮助。

我的models.py文件:


class Video(models.Model):
    Video_Description= models.CharField(max_length=500)
    slug = models.SlugField(unique=True)
    videofile= models.FileField(upload_to='deploy/videos/%Y/%m/%d/', null=True, verbose_name="")
    timestamp   = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['-timestamp']

    def get_absolute_url(self):
        return reverse ("deploy:detail", kwargs={"slug":self.slug})

    def __str__(self):
        return self.Video_Description + ": " + str(self.id)

我的views.py文件是:

class VideoDetailView(DetailView):
    queryset = Video.objects.all()



class VideoListView(ListView):

    paginate_by = 10  # <app>/<modelname>_list.html 

    def get_queryset(self, *args, **kwargs):
        qs = Video.objects.all()
        print(self.request.GET)
        query = self.request.GET.get("q", None)
        if query is not None:
            qs = qs.filter(
                Q(Video_Description__icontains=query) | Q(videofile__icontains=query))
        return qs

    def get_context_data(self, *args, **kwargs):
        context = super(VideoListView, self).get_context_data(*args, **kwargs)

        return context


video_list.html文件为:

{% extends "base.html" %}
{% load static %}

<body>
<link rel="stylesheet" href="{% static 'deploy/cafeteria_cut.mp4' %} ">

{% block content %}

{% include "result/navbar.html" %}<br/>
{% include "result/sidebar.html" %}<br/>
{% include "result/calendar.html" %}<br/>


<div style="margin-left: 10%">
<button type="button" class="btn btn-secondary btn-lg"><a href="{% url 'deploy:create' %}" style='color:white'>Upload Video</a></button>
</div>


{% for object in object_list %} 


<div class="container" style="margin-left: 10%; align-items: center;">

<h2>

</br>

<a href="{{ object.get_absolute_url }}">

{{ object.Video_Description }}

</a>

</br> 

</h2> 

</br>

<a href="" style="align-items: center;"> 

{{ object.videofile}} 

</a> 

</br> 

<br><br>
<video width='400' controls>
<source src="{% static 'deploy/youtubeVideo_cut20.mp4' %}" type='video/mp4'>
Your browser does not support the video tag.
</video>
<br><br>

 {% empty %}
            {% if request.GET.q %}
            <p style="padding-left: 20%; font-size: 70px">No Result Found </p>
            {% else %}
            <p style="padding-left: 20%; font-size: 70px"> No Result Yet.

            {% endif %}

{% endfor %} 


 {% endblock content %}

非常抱歉,当我这样做时,我没有在video_list.html文件中提及一件非常重要的事情:

<br><br>
<video width='400' controls>
<source src="{% static 'deploy/youtubeVideo_cut20.mp4' %}" type='video/mp4'>
Your browser does not support the video tag.
</video>
<br><br>

我正在浏览器上显示视频。

1 个答案:

答案 0 :(得分:0)

确保已维护媒体文件设置。在您的settings.py文件中:

MEDIA_URL = '/media/'
MEDIA_ROOT= os.path.join(os.path.dirname(BASE_DIR), "media_root")

然后在您的主urls.py中:

urlpatterns = patterns('',
    # ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

然后在您的html文件中进行更改:

<source src="{% static 'deploy/object.videofile' %}" type='video/mp4'> //here

您正在尝试将视频文件作为静态文件提取。这不是正确的方法。

尝试通过以下方式在视频的src中传递对象的视频文件的网址:

<br><br>
<video width='400' controls>
<source src="{{ object.videofile.url }}" type='video/mp4'>
Your browser does not support the video tag.
</video>
<br><br>