[英]django cannot delete audio file because it is used by audio player
I have created a django website in which users can upload music files and play them using the HTML5 audio player 我创建了一个django网站,用户可以在其中上传音乐文件并使用HTML5音频播放器播放它们
Those are my models 那是我的模特
from django.db import models
from django.db.models.signals import post_delete
from django.dispatch import receiver
# Create your models here.
class User(models.Model):
id=models.AutoField(primary_key=True)
email=models.CharField(max_length=30,unique=True,null=False,blank=False)
password=models.CharField(max_length=30,unique=True,null=False,blank=False)
name=models.CharField(unique=True,max_length=40,null=False,blank=False)
def __str__(self):
return self.name.title()+","+self.email
class Song(models.Model):
id=models.AutoField(primary_key=True)
title=models.CharField(max_length=100,null=False,blank=False)
artist=models.CharField(max_length=100,null=False,blank=False)
user=models.ForeignKey(User,null=False,blank=False)
file = models.FileField(null=False,blank=False)
def __str__(self):
return self.artist+" - "+self.title
# I can't delete the song file because it's used by the audio player in html
@receiver(post_delete, sender=Song)
def submission_delete(sender, instance, **kwargs):
instance.file.delete(False)
This the html code for displaying songs 这是用于显示歌曲的html代码
{% for song in songs %}
<div class="card">
<div class="card-body">
<h5 class="card-title">{{song.artist}} - {{song.title}}</h5>
<audio controls>
<source src="/media/{{song.file}}" type="audio/ogg" />
</audio>
<a href="/delete/{{song.id}}" onclick="confirm_delete_song('/delete/{{song.id}}')" class="delete" > <i class="material-icons" title="Supprimer"></i></a>
</div>
</div>
{% endfor %}
And the view that deletes the song 和删除歌曲的视图
def delete(request, song):
if 'logged' not in request.session:
return HttpResponseRedirect('/')
else:
try:
Song.objects.get(id=song).delete()
return HttpResponseRedirect('/')
except:
return HttpResponseRedirect('/')
A screenshot of the page 页面截图
When I click on the delete button, the song is removed from the database but the file is not deleted even if I haven't played the audio yet 当我单击删除按钮时,歌曲将从数据库中删除,但是即使我尚未播放音频,文件也不会被删除
After some debugging, it seems that the audio player is using the audio file which is why django cannot delete it 经过一些调试后,似乎音频播放器正在使用音频文件,这就是django无法删除它的原因
[WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\Amine\\Desktop\\omp\\media\\remix.mp3' [WinError 32]该进程无法访问文件,因为该文件正在被另一个进程使用:'C:\\ Users \\ Amine \\ Desktop \\ omp \\ media \\ remix.mp3'
Any help would be appreciated 任何帮助,将不胜感激
Try this, 尝试这个,
def delete(request, song):
if 'logged' not in request.session:
return HttpResponseRedirect('/')
else:
try:
song = Song.objects.get(id=song) song.file.delete() # File delete song.delete # object delete
return HttpResponseRedirect('/')
except:
return HttpResponseRedirect('/')
I was finally able to fix the issue by using javascript to set the src attribute of the audio tag to an empty string just before deleting. 我终于能够在删除之前通过使用javascript将音频标签的src属性设置为空字符串来解决此问题。 This caused the file to be free
这导致文件免费
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.