I am in new in django and i have a model "album" which has 3 fileds title , genre and rating and i am displaying them i a table and i want to display the digit"0" as many time as album.rating and i am using for loop from 0 to album.rating but it is displaying only once i.e if the album.rating is 2 then "0" should display only 2 times but in my case it is displaying only 1 time .Please help me.Thanks in advance.
Here is the code of the html -
{% if albums %}
{% for album in albums %}
<tbody>
<tr>
<td>{{ album.album_title }}</td>
<td>{{ album.genre }}</td>
<!-- rating stars -->
<td>
{% for i in album.rating %}
<option value={{i}}>0</option>
{% endfor %}
</td>
<td>
<a href="{% url 'music:edit' album.id %}" class="btn btn-primary btn-sm" role="button">Edit</a>
</td>
<td>
</td>
</tr>
</tbody>
Here is the code of view.py
def index(request):
if not request.user.is_authenticated():
return render(request, 'music/login.html')
else:
albums = Album.objects.filter(user=request.user)
paginator = Paginator(albums, 2) # Show 25 contacts per
page = request.GET.get('page')
try:
albums = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
albums = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
albums = paginator.page(paginator.num_pages)
song_results = Song.objects.all()
query = request.GET.get("q")
if query:
albums = albums.filter(
Q(album_title__icontains=query) |
Q(artist__icontains=query)
).distinct()
song_results = song_results.filter(
Q(song_title__icontains=query)
).distinct()
return render(request, 'music/index.html', {
'albums': albums,
'songs': song_results,
})
else:
return render(request, 'music/index.html', {'albums': albums})