简体   繁体   English

django无法删除音频文件,因为它被音频播放器使用

[英]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">&#xE872;</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.

相关问题 Django 音乐播放器:按下播放按钮时,models.FileField 未正确将音频文件传递给 HTML - Django music player: models.FileField not properly passing audio file to HTML when Play button is pressed 如何使用Django映射音频文件? - How to map an audio file with Django? 无法打开目录中的音频文件 - Cannot open audio file in the directory Python / Tkinter音频播放器 - Python/Tkinter Audio Player 从 blob 音频 django 创建一个 wav 文件 - Create a wav file from blob audio django 无法在 django 后端发送音频文件 - unable to send audio file in django backend WinError 32进程无法访问该文件,因为另一个进程正在使用该文件无法通过python或手动删除文件 - WinError 32 The process cannot access the file because it is being used by another process cannot delete file through python or manually 我刚使用Python 3.0拍摄后无法删除屏幕截图(该进程无法访问该文件,因为该文件正在被另一个进程使用) - Can't delete screenshot after I just took it using Python 3.0 (The process cannot access the file because it is being used by another process) 在 Django 应用程序中通过音频标签播放音频 - Playing audio through audio tag in a Django application python 使用语音识别后无法删除音频文件 - python after using speech recognition can´t delete the audio file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM