我想保存来自youtube的视频的url字段,以便我可以将其加载到我的网站中。到目前为止,我想出了这个在HTML中:django - 使用正则表达式去掉和替换urlfield
<h3>{{video.title}}</h3>
<object width="425" height="344"><param name="movie" value="{{video.video_url}}"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="{{video.video_url}}" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>
但问题是,它只能以一种方式工作。例如:
网址:http://www.youtube.com/watch?v=e4lHTj9xFqE
如果watch?
的条纹这只会工作,=
成为/
这样最终网址会有点像这个
http://www.youtube.com/v/e4lHTj9xFqE
我在想的在保存之前使用正则表达式并替换并去除网址。我该如何去掉watch?
,然后用/
替换=
?还有没有更好的方式来加载在HTML中的视频?来自你们的建议将非常感谢。谢谢!
编辑:
models.py:
class Video(models.Model):
title = models.CharField(max_length=100)
video_url = models.URLField(max_length=100)
def save(self, *args, **kwargs):
new_url = (self.video_url.replace("watch?v=","v/"))
super(Video, self).save(*args, **kwargs)
if new_url:
self.video_url = new_url
工作,感谢您对两个答案!我确实尝试过,但我得到错误。我已经添加了我的models.py。请你看看它。 – Robin
正则表达式也不能代替它。 – Robin
@Robin现在看到.. –