using Listview to paginate forms of a formset

Hello

I am using a ListView to display and select forms within a formset.

The code for the get is the following

 def get_initial(self):
        """create a list of initial values for each element of the object_list"""
        initial = [bmf.StitchTechniqueRepresentation(mst_pk, self.member).to_dict() for mst_pk in self.pks]
        return initial
    
    def get_queryset(self):
        return self.formset.forms

    def get(self, request, *args, **kwargs):
        form_class = self.form_class
        self.formset = form_class(**self.get_form_kwargs())
        #all_names = MachineSTMixin.all_names(self.member)
        #setattr(self.formset, 'all_names', all_names)
        return super().get(self.request, *args, **kwargs)
    
    def get_form_kwargs(self):
        kwargs = {'initial': self.get_initial()}                  
        if self.request.method in ('POST', 'PUT'):
            kwargs.update({
                'data': self.request.POST,
                'files': self.request.FILES,
            })
        return kwargs

In the template I have

<form method="post" enctype="multipart/form-data">
  {% csrf_token %}

<div class="container px-0">
  <div class="d-flex flex-column gap-3">

    {% for form in object_list %}
      {% include "app_fabric/_st_row_form.html" with form=form actions_json='[{"label":"display SFD","href":"/app_fabric/displaySFD/{id}"},{"label":"edit ST","href":"/app_fabric/editST/{id}"}]'%}
    {% empty %}
      <div class="alert alert-light border mb-0">{% translate "No results." %}</div>
    {% endfor %}

  </div>

I neef to access fields of the form individually in the template and variables such as {{form.fieldname}} seems to be empty.

In my IDE I get builtins.AttributeError MySTForm has no attribute field name.

MySTForm is the class of the form in the formset.

an someone tell me why?

From the docs for ListView:

A page representing a list of objects.

Objects, not forms (or a formset). The Django-provided ListView is not the base class you should be using for this.

Likewise, the Django-provided Paginator class wasn’t designed to handle formsets. (I see a number of potential problems of doing it, particularly in the area of managing the management form.)

I think this is something you would need to manage manually. If each page transition requires a ‘get’ of the next page, then you would need to submit the contents of the current page - otherwise, any entries would be lost. And, if you’re going to submit that data, the management form needs to be correct for that page.

If you’re looking to allow for multiple “pages” within a formset, you’re probably better off performing all the “page” management within the browser using JavaScript.

Hello

I forgot to mention that the formset has no possibility to add or delete a form in the formset. It is just a way to display a list of identical objects. Moreover each form contains several information to display but only 2 fields to fill and return in the post.

That doesn’t affect any of the above.

As soon as you change from “display only” to “edit something” - no matter how much (or little) that “something” may be, you need to change your approach.