i’m making a music streaming website and i’m trying to display the user’s list of playlists in the , but i always get that the user has no playlists(but there are playlists in database for the user). What is the problem? maybe the playlist view is not correct?
html:
<div id="divId">
{% if myPlaylists %}
<p style="margin: 5px; font-size: 0.75rem; font-weight: 700; color: rgb(138, 138, 138); text-align: left;">Add This Song To Your Playlist</p>
{% for playlist in myPlaylists %}
<a class="left-panel-btn" id="D_{{playlist.playlist_id}}" onclick="sendPostRequestPlaylistSong(this)">{{playlistInfo.playlist_name}}</a><br>
{% endfor %}
{% else %}
<p style="margin: 5px; font-size: 0.8rem; font-weight: 700; color: rgb(138, 138, 138); text-align: left;">You don't have any Playlist.</p>
<a class="create-p" onclick="$('#divId').css({display: 'none'}); document.getElementById('modal-wrapper-playlist').style.display='block'"><i class="fas fa-plus"></i> Create Playlist</a>
{% endif %}
</div>
models.py:
class Song(models.Model):
song_id = models.AutoField(primary_key=True)
name = models.CharField(max_length=50)
artist = models.CharField(max_length=50)
album = models.CharField(max_length=50, blank=True)
genre = models.CharField(max_length=20, blank=True, default='Album')
song = models.FileField(upload_to="songs/", validators=[FileExtensionValidator(allowed_extensions=['mp3', 'wav'])], default="name")
image = models.ImageField(upload_to="songimage/", validators=[FileExtensionValidator(allowed_extensions=['jpeg', 'jpg', 'png'])], default="https://placehold.co/300x300/png")
data = models.DateTimeField(auto_now=False, auto_now_add=True)
slug = models.SlugField()
def __str__(self):
return self.name
class Meta:
ordering = ['name']
class Playlist(models.Model):
playlist_id = models.AutoField(primary_key=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
playlist_name = models.CharField(max_length=50, default="name")
image = models.ImageField(upload_to="playlist_images/", validators=[FileExtensionValidator(allowed_extensions=['jpeg', 'jpg', 'png'])], default="media/images/music-placeholder.png")
plays = models.IntegerField(default=0)
songs = models.ManyToManyField(Song, related_name='playlist')
slug = models.SlugField()
def __str__(self):
return self.playlist_name
views.py:
def playlist(request, playlist_id):
user = request.user
if user.is_authenticated:
myPlaylists = Playlist.objects.filter(user=user)
if request.method == "POST":
song_id = request.POST["music_id"]
playlist = Playlist.objects.filter(playlist_id=playlist_id).first()
if song_id in playlist.songs.all():
playlist.songs.remove(song_id)
playlist.plays -= 1
playlist.save()
message = "Successfull"
print(message)
return HttpResponse(json.dumps({'message': message}))
else:
images = os.listdir("media/playlist_images")
print(images)
currPlaylist = Playlist.objects.filter(playlist_id=playlist_id).first()
songs = currPlaylist.songs
playlistSongs = [currPlaylist.songs.all()]
recommendedSingers = []
return render(request, "music/playlist.html", {'playlistInfo': currPlaylist,
'playlistSongs': playlistSongs,
'myPlaylists': myPlaylists,
'recommendedSingers': recommendedSingers})
2
Answers
I added to the homepage.view the list of playlist like this:
Without debug it is hard to answer.
please provide the print result of those code for user with existed playlists:
By the way – you have an error in template: