Formset submit only one form

Hello!

I am using formset to display a list of customers. I would like to add a button to the display (1 for each customer) that would redirect to the customers detail page. I can make the button call the proper view, but I can only seem to pass the formset zero index data. Is there a way to get a specific formset index to submit? Or is formset intended only to do all at once?

1 Like

If you’re only using the formset to display a list of customers (and not to edit those customers), then you’re using the wrong tool. (Yes, a formset is designed to submit the entire formset. Yes, there is a way of short-circuiting that to do what you’re looking for, but that’s probably not what you really want to do here.)

A display list is best presented as a ListView, or something comparable to that.

Hey Ken,

I actually am using the formset to update/delete/create customers. I just want to add a button that will pass one of the formset forms data (customer_id) to another view to display customer details. I am displaying the formset in a table where the user can update information, and a check box in the table row to mark one form for deletion. I want this “view customer” button to exist in the table (1 for each row).

Ok, so it sounds like you don’t want to submit the form at that point - you just want to render a link to that details page.

If you’re really trying to submit that one form and transfer you to a different page, then you’re probably going to have to write a bit of JavaScript to manage that, since the form submit is going to POST the form data to the view that handles the formset, and it will redirect you to where it thinks you should go.

Gotcha!
Thanks Ken. You are correct I dont want to submit the entire formset, just 1 form and send its customer_id attribute to another view.

Keep in mind that from the perspective of the html, the “formset” (a logical Django structure) is only one html form - it is not physically a set of forms. For you to only submit 1 “form” means that you need to extract those fields from the form and submit them independently of the rest of the form data - probably the easiest way to do that would be to submit it API-style as a JSON object.

Hello,
I had a similar requirement. Following worked well for me.

In the django template , the id of the form (model record passed from .views) is available through instance.

you can use form.instance.id as the parameter for your url name in the template.
You can use this in the href tag with url.

Please see below as illustration.

{{ formset.management_form }}
{% for form in formset %}

                    <fieldset class="form-group">
                        {% if form.instance.id %}
                        <button class="btn">
                            <a class="btn btn-secondary" href="{% url 'qsd-order-itemset' form.instance.order.id form.instance.id|default:None %}">Copy Order {{form.instance.order.id}} line {{ form.instance.id }}</a>
                        
                        </button>
                        {%  else  %}
                        <p> new to be added</p>
                        {% endif %}

…rest of the code…
…