請與我裸露,我是新來的Django/Python的,所以很可能是一個簡單的解決我的問題......返回Django中的文件路徑沒有文件擴展名
我創建我的Django網站上的多媒體應用程序允許用戶上傳照片,視頻和音頻 - 文件在第三方服務(mp4,ogg,webm和flv for videos)上編碼,然後保存到Amazon 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))
這太棒了,謝謝!我唯一的問題是,它返回的圖像的完整路徑,但我需要指定一個單獨的靜態URL ..是否有可能只返回文件名?舉例來說: static.myurl.com/username/videos/foo.mp4 會返回: foo – 2012-01-11 19:01:10