how to get the last file uploaded to templates

i want to get the last file i was upload and it will be played in templates. but what i make it, every file i was upload, it can be played (so there’s so many audio player). what i want is just show 1 audio player with the last audio i was upload it.

here’s my code html:

{% for record in audio %}
     <audio controls="controls">
            <source src="/media/mp3/{{record.audio}}" type="audio/mpeg">
     </audio>
{% endfor %}

views.py:

def homepage(request):
    form = AudioForm()
    audio = Audio_store.objects.all()
    if request.method == "POST":
        form = AudioForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
        return redirect("homepage")

    context={'form':form, 'audio':audio}
    print(audio)
    return render(request, "homepage.html", context=context)

Are you using the other audio items elsewhere in your template? If not, you should only fetch the one audio instance you want from the db.

If you are using them in a few places, I’d recommend making the audio context variable a list so that you don’t have to worry about re-executing the query.

    ...
    context={'form':form, 'audio': list(audio)}
    return render(request, "homepage.html", context=context)

Then in your template you can use the last template filter

     <audio controls="controls">
            <source src="/media/mp3/{{audio|last.audio}}" type="audio/mpeg">
     </audio>