【问题标题】:how to display multiple videos in django如何在 django 中显示多个视频
【发布时间】:2022-01-24 09:00:28
【问题描述】:

这里的代码运行良好,但我想显示多个视频。这里只显示一个视频。 上传第二个视频时,第二个视频文件正在存储但不显示。 请帮我解决这个问题。请。

views.py:

def showvideo(request):
    lastvideo= Video.objects.all()
    form= VideoForm(request.POST or None, request.FILES or None)
    if form.is_valid():
        form.save()
    context= {'lastvideo': lastvideo,
              'form': form
              }
    return render(request, 'master/video.html', context)

forms.py:

 class VideoForm(forms.ModelForm):
        class Meta:
            model= Video
            fields= ["name", "videofile"]

models.py:

 class Video(models.Model):
        name= models.CharField(max_length=500)
        videofile= models.FileField(upload_to='videos/', null=True, verbose_name="")
    
        def __str__(self):
            return self.name + ": " + str(self.videofile)

video.html:

 <html>
    <head>
    <meta charset="UTF-8">
    <title>Upload Videos</title>
    </head>
    <body>
    
    <h1>Video Uploader</h1>
    <form enctype="multipart/form-data" method="post" action="">
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Upload"/>
    </form>
    
    <br><br>
    <video width='400' controls>
    <source src='{{videofile.url}}' type='video/mp4'>
    Your browser does not support the video tag.
    </video>
    <br><br>
    </p>
    
    </body>
    <script>'undefined'=== typeof _trfq || (window._trfq = []);'undefined'=== typeof _trfd && (window._trfd=[]),_trfd.push({'tccl.baseHost':'secureserver.net'}),_trfd.push({'ap':'cpbh-mt'},{'server':'p3plmcpnl487010'},{'id':'8437534'}) // Monitoring performance to make your website faster. If you want to opt-out, please contact web hosting support.</script>
    <script src='https://img1.wsimg.com/tcc/tcc_l.combined.1.0.6.min.js'></script>
    </html>

urls.py:

path('videos/',views.showvideo,name='showvideo'),

【问题讨论】:

    标签: django


    【解决方案1】:

    在您看来,您是在保存前调用查询视频,请将其更改为:

    def showvideo(request):
        form= VideoForm(request.POST or None, request.FILES or None)
        if form.is_valid():
           form.save()
        
        context= {'lastvideo': Video.objects.all(),
              'form': form
              }
        return render(request, 'master/video.html', context)
    

    【讨论】:

    • 只显示一个最后上传的视频
    • 查看视频文件的绝对路径是不同还是相同?
    • 不一样
    • 当我添加上面的代码时它没有查看
    【解决方案2】:

    这里,如果我们要显示多个视频,那么在这个模板中添加for循环

    <html>
            <head>
            <meta charset="UTF-8">
            <title>Upload Videos</title>
            </head>
            <body>
            
            <h1>Video Uploader</h1>
            <form enctype="multipart/form-data" method="post" action="">
            {% csrf_token %}
            {{ form.as_p }}
            <input type="submit" value="Upload"/>
            </form>
            
            <br><br>
            {% for video in lastvideo %}
            <tr>
             <td>
                <video width='400' controls>
                <source src='{{video.videofile.url}}' type='video/mp4'>
                 Your browser does not support the video tag.
                 </video>
             </td>
             </tr>
            {% endfor %}
            <br><br>
            </p>
            
            </body>
            <script>'undefined'=== typeof _trfq || (window._trfq = []);'undefined'=== typeof _trfd && (window._trfd=[]),_trfd.push({'tccl.baseHost':'secureserver.net'}),_trfd.push({'ap':'cpbh-mt'},{'server':'p3plmcpnl487010'},{'id':'8437534'}) // Monitoring performance to make your website faster. If you want to opt-out, please contact web hosting support.</script>
            <script src='https://img1.wsimg.com/tcc/tcc_l.combined.1.0.6.min.js'></script>
            </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-21
      • 1970-01-01
      • 2021-04-11
      • 2014-11-24
      • 1970-01-01
      • 1970-01-01
      • 2018-02-17
      相关资源
      最近更新 更多