[Answer]-Unable to access upvote/downvote input in a Django template

1👍

type="input" is not a valid button type. I’d get rid of the hidden input altogether, and change the buttons to inputs:

<form method="POST" action="{% url 'vote' %}" class="vote_form">
    {% csrf_token %} 

    <input type="submit" name="btn1" value="upvote">
    <input type="submit" name="btn1" value="downvote">
</form> 

Then, in your view, you can use:

if request.method == 'POST':
    btn1 = request.POST.get('btn1')

    if btn1 == 'upvote':
        # Do the upvote
    elif btn1 == 'downvote':
        # Do the downvote

Leave a comment