Andi Fathul Mukminin
Andi Fathul Mukminin

Reputation: 71

Htmx hx-post does not contain the name of the button that submitted the form despite having name and value

I have Django form that have 2 different submit button, i need to know which button is clicked. But the button did not include in request.POST ( I am using htmx to issue a POST request )

This is my form:

<form hx-post="{% url 'recomposition:akb-edit' %}" hx-target="#target-{{this_month}}-{{data.pk}}" hx-swap="outerHTML" enctype="multipart/form-data">
 ...
    <div class="form-group modal-footer p-0">
        <div class="btn-group d-flex w-100" role="group" aria-label="...">
            {% if this_month != 0 %}
            <button type="submit" value="delete" name="delete" class="btn btn-danger w-100" {%if not is_month %}disabled{% endif %}>Delete changes</button>
            {% endif %}
            <button type="button" class="btn btn-secondary w-100" onclick="closeModal()">Close</button>
            <button type="submit" value="save" name="save" class="btn btn-primary w-100" >Save changes</button>
        </div>
    </div>
</form>

I didnt know whats wrong the last time I encounter this if i forgot to included the name but now i have the name and value but id didnt show up in request.POST

Upvotes: 1

Views: 1933

Answers (1)

Marco Staffoli
Marco Staffoli

Reputation: 2496

I suggest you to use the same name for the two submit button and then to retrieve the value of btn parameter:

<button type="submit" name="btn" value="delete">Delete changes</button>
<button type="submit" name="btn" value="save">Save changes</button>

Upvotes: 0

Related Questions