if i post a song from my admin panel, i want the list of song to appear in the templates, i already created a models that allow me to post it, but i don't know how to create a views that allowed the song to appear in the templates, please how can i do this ?
this is my empty views views.py
from django.shortcuts import render
from .models import Audio
# Create your views here.
def index(request):
return render(request, 'index.html')
this is my models.py i created models.py
from django.db import models
# Create your models here.
class Audio(models.Model):
book_title = models.CharField(max_length=100, null=False, blank=False)
file = models.FileField(upload_to='file')
author = models.CharField(max_length=100, null=False, blank=False)
artist = models.CharField(max_length=100, null=False, blank=False)
You pass a queryset of Audio objects to the template:
# Create your views here.
def index(request):
context = {'songs': Audio.objects.all()}
return render(request, 'index.html', context)
in the template we can enumerate over the songs:
{% for audio in songs %}
{{ audio.book_title }} by {{ audio.artist }} </br>
{% endfor %}