Ora posso caricare singoli URL di video di youtube. Ma il problema ora è caricare i video delle playlist di YouTube. Quindi la mia domanda è, come faccio a sostituire due stesso modello, ma entrambi con sostituzioni diverse di un URL?Django - sostituisci le parti del tuo urlfield
Esempio:
URL reale:
<iframe width="400" height="327" src="http://www.youtube.com/embed/1UiICgvrsFI&list=PLvAOICvbvsbnc5dLG0YR9Mq_tFfzAhQSp&index=1" allowfullscreen="true"></iframe>
Sostituire il modello a diventare come questo:
<iframe width="560" height="315" src="//www.youtube.com/embed/1UiICgvrsFI?list=PLvAOICvbvsbnc5dLG0YR9Mq_tFfzAhQSp" allowfullscreen></iframe>
Ecco i primi &
modifiche ?
e la seconda &
e le sue seguenti contenuti cioè &index=1
sono spogliati.
Questa è la 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
Edit:
def save(self, *args, **kwargs):
new_url = re.sub('watch\?v=','embed/',self.video_url)
new_url = re.sub(r'^(http:\/\/)([\w\W]+)\&list=([\w\W]+)(\&index=[\d]+)$', r'//\2?list=\3', new_url)
if new_url:
self.video_url = new_url
super(Video, self).save(*args, **kwargs)
Grazie per la risposta. L'ho provato, ma c'è ancora un '&' nell'URL. Per favore, potresti mostrarmi come farlo in regex? E per favore nota che indice = 1 può variare. Grazie ancora. – Robin
C'è ancora un '' nell'URL e 'http: //': http://www.youtube.com/embed/1UiICgvrsFI'&'list=PLvAOICvbvsbnc5dLG0YR9Mq_tFfzAhQSp – Robin
Funziona nella console, ma posso ' Sembra che funzioni nel mio progetto ... Per favore, guarda la modifica, ho aggiunto anche il tuo snippet. – Robin