pythonhtmldjangobackend

My Django for-loop is not displaying the elements


I'm making a loop to display all the songs in my music streaming website homepage with a carousel but is not working (it doesn't display nothing but i have like 10 songs in my database). How can i fix it? This is my html code:

<div id="All-songs" style="padding: 5px">
    <h2>All Songs</h2>
    <div class="main-carousel" data-flickity='{"groupCells":5 , "contain": true, "pageDots": false, "draggable": false, "cellAlign": "left", "lazyLoad": true}'>
        {% for i in allsongs %}
            <div class="carousel-cell">
                <section class="main_song">
                    <div class="song-card">
                        <div class="containera">
                            <img src="{{i.image}}" id="A_{{i.id}}" alt="song cover">
                            <div class="overlaya"></div>
                            <div>
                                <a class="play-btn" href="{{i.preview_url}}" id="{{i.id}}"><i> class="fas fa-play-circle"></i></a>
                                {% if user.is_authenticated %}
                                    <div class="add-playlist-btn">
                                        <a id="W_{{i.song_id}}" title="Add to Playlist" onclick="showDiv(this)"></a>
                                    </div>
                                {% endif %}
                            </div>
                        </div>
                    </div>
                    <div>
                        <p class="songName" id="B_{{i.id}}">{{i.name}}</p>
                    </div>
                    <p class="artistName">{{i.singer1}}</p>
                </section>
            </div>
        {% endfor %}
    </div>
</div>

this is views.py:

def allSongs(request):
allsongs = list(Song.objects.all())
random.shuffle(allsongs)
return render(request, 'allSongs.html', {'allsongs': allsongs)

and this is 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="media/songs/", validators=[FileExtensionValidator(allowed_extensions=['mp3', 'wav'])], default="name")
image = models.ImageField(upload_to="media/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(unique=True)

def __str__(self):
    return self.name

class Meta:
    ordering = ['name']

Solution

  • I solved the problem integrating the allSongs view(in the app views.py) to the homepage view(in the project views.py) like this:

    def homepage(request):
         songs = Song.objects.all()
         return render(request, 'homepage.html', {'songs': songs})