Passing HTML tag data- to Django with form submit

I am trying to pass data in HTML tag with a form submit to Django

I currently have a template like this:

{% for item in items %}
    <div class="product">
        {{ item.item_name }}
            <form method="POST">
                {% csrf_token %}
                <input
                    type="submit"
                    value="add to cart"
                    data-pk="{{item.pk}}"
                    class="add_product"/>
            <form>
    </div>
{% endfor %}

I want to pass data-pk with a Django form. How do I do that? Also I know, I can create a view to handle endpoint to do that, and include pk in the url, is that a better solution?

I am new to Django, and I will be grateful for any input on how to do stuff the right way

Instead of data, use value={{item.pk}} and handle the submit logic in your view.

Back to Top