B
B
bielikovv2022-04-13 11:51:55
Django
bielikovv, 2022-04-13 11:51:55

How to output audio file from ManyToManyField?

I have a playlist model that has a manytomany relationship with the song model. Initially, I access a specific playlist through the get method in the view. Next, I access this playlist and get all the songs in this playlist via playlist.song.all(). There are some problems with the output in the template. Tried and framed in if tags, the error is most likely in the loop. Point me to it please

views.py

def show_playlist(request, playlist_id):
    playlist = Playlists.objects.get(pk=playlist_id)
    songs = playlist.song.all()
    return render(request, 'musiccloud/current_playlist.html', {'playlist':playlist, 'songs':songs})


models.py
class Playlists(models.Model):
    user = models.ForeignKey(User, on_delete=models.PROTECT, verbose_name='Пользователь', null=True)
    title = models.CharField(max_length=128, verbose_name='Название Плейлиста')
    envelope = models.ImageField(verbose_name='Обложка плейлиста', upload_to='photo/%Y/%m/%d')
    date_playlist = models.DateField(auto_now_add=True, verbose_name='Дата создания плейлиста')
    song = models.ManyToManyField('Music', verbose_name='Песня', blank=True, null=True)

    def __str__(self):
        return self.title

class Music(models.Model):
    photo = models.ImageField(verbose_name='Обложка', upload_to='photo/%Y/%m/%d', blank=True)
    title = models.CharField(max_length=455, verbose_name='Название', default='Без названия', blank=True)
    person = models.CharField(max_length=255, verbose_name='Исполнитель', default='Неизвестный исполнитель', blank=True)
    file = models.FileField(verbose_name='Песня', upload_to='audio')
    album = models.ForeignKey(Album, verbose_name='Альбом', blank=True, on_delete=models.CASCADE, null=True)
    date_song = models.DateField(auto_now_add=True, verbose_name='Дата релиза песни', null=True)

    def __str__(self):
        return self.title


Piece of html template
<div class="container">
    <div class="row">
        <div id="music-current-card" class="music-current-card">
            <div class="card-body">
                <div id="playlist-current-card" class="playlist-card">
                    <h1 class="card-title">{{ playlist.title }}</h1>
                    {% if playlist.envelope %}
                    <img id="envelope-current-playlist" src="{{ playlist.envelope.url }}">
                    {% endif %}
                    {% for music in songs %}
                    <audio src="{{ music }}"></audio>
                    {% endfor %}
                </div>
            </div>
        </div>
    </div>
</div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2022-04-13
@bielikovv

You just output music, why did you decide that this will display a link to the file? You have already done a similar operation with "Playlist Cover", do the same here.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question