[Answer]-Playing audio through audio tag in a Django application

1👍

Based on your settings, you cannot play the file because:

  • {{ file.audio_file }} returns value saved in the database, which is absolute path to media file, e.g. http://localhost:8000/Users/test/django_app/media/test.wav

To solve this, you need to:

  1. Edit your models.py

    class AudioFile(models.Model):
    audio_file = models.FileField(upload_to="music")

    upload_to — A path that will be appended to your MEDIA_ROOT setting to determine the value of the url attribute

  2. In your template, use {{ file.audio_file.url }} to correctly reference the URL of the uploaded file

  3. You may need to delete existing files, because wrong path was saved in the database.

Leave a comment