在 Django 中返回不带文件扩展名的文件路径
我正在 Django 网站上创建一个多媒体应用程序,允许用户上传照片、视频和音频 - 文件在第 3 方服务上进行编码(视频为 mp4、ogg、webm 和 flv),然后保存到用户的存储桶中在亚马逊 S3 上。我正在专门寻找一种优雅的解决方案来以 HTML5 格式显示用户的视频。这是我的模型的外观:
class Video(models.Model):
file = models.FileField(upload_to = user_video_folder)
owner = models.ForeignKey(User)
video_title = models.CharField(max_length=100)
video_caption = models.CharField(max_length=150, blank=True)
date_added = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.video_title
然后是视图:
def ViewAllVideos(request):
videos = Video.objects.filter(owner = request.user)
template = 'site/media-videos.html'
return render_to_response(template, {'videos': videos}, context_instance=RequestContext(request))
在模板中,我需要打印编码期间生成的多种文件格式的路径,并且希望不必为每个添加一列格式在我的模型中,所以我想知道是否有一种方法可以返回带有文件名的文件路径,但不带扩展名,所以它会像模板中那样:
<video width="900" height="506" preload="" controls="" autoplay="">
<source src="{{ STATIC_URL }}{{ video.file }}.mp4" type="video/mp4;">
<source src="{{ STATIC_URL }}{{ video.file }}.ogg" type="video/ogg;">
<source src="{{ STATIC_URL }}{{ video.file }}.webm" type="video/webm;">
<object width="900" height="506" type="application/x-shockwave-flash" data="/foo/bar/flowplayer/flowplayer-3.2.5.swf">
<param name="movie" value="/foo/bar/flowplayer/flowplayer-3.2.5.swf"><param name="allowfullscreen" value="true">
<param value="config={"clip": {"url": "{{ STATIC_URL }}{{ video.file }}.flv", "autoPlay":false, "autoBuffering":true}}" name="flashvars">
</object>
</video>
感谢任何帮助!干杯!
这是我的新观点:
def ViewAllVideos(request):
videos = Video.objects.filter(owner = request.user)
filenames = [os.path.splitext(os.path.basename(video.file))[0]
for video in videos]
context = {
'videos': videos,
'filenames': filenames,}
template = 'site/media-videos.html'
return render_to_response(template, context, context_instance=RequestContext(request))
I'm creating a multimedia application on my Django site that allows users to upload photos, videos and audio - the files get encoded on a 3rd party service (mp4, ogg, webm and flv for videos), and then saved to the user's bucket on Amazon S3. I'm specifically looking for an elegant solution to display the user's videos in HTML5 format. Here's how my model looks:
class Video(models.Model):
file = models.FileField(upload_to = user_video_folder)
owner = models.ForeignKey(User)
video_title = models.CharField(max_length=100)
video_caption = models.CharField(max_length=150, blank=True)
date_added = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.video_title
Then the view:
def ViewAllVideos(request):
videos = Video.objects.filter(owner = request.user)
template = 'site/media-videos.html'
return render_to_response(template, {'videos': videos}, context_instance=RequestContext(request))
In the template, I need to print the path for the multiple file formats that are generated during encoding, and was hoping to not have to add a column for each format in my model, so I was wondering if there was a way to return the file path WITH the file name, but without the extension, so it would like like so in the template:
<video width="900" height="506" preload="" controls="" autoplay="">
<source src="{{ STATIC_URL }}{{ video.file }}.mp4" type="video/mp4;">
<source src="{{ STATIC_URL }}{{ video.file }}.ogg" type="video/ogg;">
<source src="{{ STATIC_URL }}{{ video.file }}.webm" type="video/webm;">
<object width="900" height="506" type="application/x-shockwave-flash" data="/foo/bar/flowplayer/flowplayer-3.2.5.swf">
<param name="movie" value="/foo/bar/flowplayer/flowplayer-3.2.5.swf"><param name="allowfullscreen" value="true">
<param value="config={"clip": {"url": "{{ STATIC_URL }}{{ video.file }}.flv", "autoPlay":false, "autoBuffering":true}}" name="flashvars">
</object>
</video>
Any help is appreciated! Cheers!
Here's my new view:
def ViewAllVideos(request):
videos = Video.objects.filter(owner = request.user)
filenames = [os.path.splitext(os.path.basename(video.file))[0]
for video in videos]
context = {
'videos': videos,
'filenames': filenames,}
template = 'site/media-videos.html'
return render_to_response(template, context, context_instance=RequestContext(request))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将如下内容添加到您的模型中,然后使用
{{video.file_minus_extension}}
编辑:根据评论,只需返回文件名
You could add something like the below to your model, then use
{{video.file_minus_extension}}
EDIT: as per comment, just return filename
我认为您正在寻找的内容类似于:
注意:这应该在您的视图中执行,并将返回值传递给您的模板。
例如,在您看来,您可以执行以下操作:
我认为您只是在模板中使用
video.file
,所以也许您甚至可以不将其传递给模板,因为它不会'不需要。I think that what you're looking for is something like:
Note: This is supposed to be executed in your view and the returned value passed to your template.
For example, in your view you could do something like:
I think you're just using
video.file
in your template, so maybe you can even don't pass it to the template since it won't be needed.