I am working on a project in django. I have this part of code, when I want to delete an album, it shows Yes or No to choose, but the problem is, whatever the choice I make, it always delete the album. I know I have to add something to the confirmation option but I don't want where or what.
<form action="{% url 'music:delete_album' album.id %}" method="post" style="display: inline;">
{% csrf_token %}
<input type="hidden" name="album_id" value="{{ album.id }}" />
<button type="submit" onclick="confirm('Are you sure ?')" class="btn btn-default btn-sm">
<span class="glyphicon glyphicon-trash"></span>
</button>
</form>
and this is the delete_album view :
def delete_album(request, album_id):
album = Album.objects.get(pk=album_id)
album.delete()
albums = Album.objects.filter(user=request.user)
return render(request, 'music/index.html', {'albums': albums})
Ok, It could be validated before to submit your form. If you copy the next snipped then It will validate :)
<form action="{% url 'music:delete_album' album.id %}"
method="post" style="display: inline;" onsubmit="window.mytest()">
{% csrf_token %}
<input type="hidden" name="album_id"
value="{{ album.id }}" />
<button type="submit" class="btn btn-default btn-sm">
<span class="glyphicon glyphicon-trash"></span>
</button>
</form>
<script type="text/javascript">window.mytest = function() { var isValid = confirm('Are you sure ?');if (!isValid) { event.preventDefault(); alert("It wont delete. Yay!");}}</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With