Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a generic example of the solution I found to my problem:</p> <p>In the <code>forms.py</code> file:</p> <pre><code>class MyForm(ModelForm): class Meta: model = MyModel fields = ('description',) </code></pre> <p>In the <code>views.py</code> file:</p> <pre><code>FormSet = modelformset_factory(MyModel, form=MyForm, extra=0) if request.method == 'POST': formset = FormSet(request.POST, request.FILES) # Your validation and rest of the 'POST' code else: query = MyModel.objects.filter(condition) paginator = Paginator(query, 10) # Show 10 forms per page page = request.GET.get('page') try: objects = paginator.page(page) except PageNotAnInteger: objects = paginator.page(1) except EmptyPage: objects = paginator.page(paginator.num_pages) page_query = query.filter(id__in=[object.id for object in objects]) formset = FormSet(queryset=page_query) context = {'objects': objects, 'formset': formset} return render_to_response('template.html', context, context_instance=RequestContext(request)) </code></pre> <p>You need to create the formset with the objects in the present page, otherwise, when you try to do <code>formset = FormSet(request.POST, request.FILES)</code> in the POST method, Django raises a MultiValueDictKeyError error.</p> <p>In the <code>template.html</code> file:</p> <pre><code>{% if objects %} &lt;form action="" method="post"&gt; {% csrf_token %} {{ formset.management_form }} {% for form in formset.forms %} {{ form.id }} &lt;!-- Display each form --&gt; {{ form.as_p }} {% endfor %} &lt;input type="submit" value="Save" /&gt; &lt;/form&gt; &lt;div class="pagination"&gt; &lt;span class="step-links"&gt; {% if objects.has_previous %} &lt;a href="?page={{ objects.previous_page_number }}"&gt;Previous&lt;/a&gt; {% endif %} &lt;span class="current"&gt; Page {{ objects.number }} of {{ objects.paginator.num_pages }} &lt;/span&gt; {% if objects.has_next %} &lt;a href="?page={{ objects.next_page_number }}"&gt;next&lt;/a&gt; {% endif %} &lt;/span&gt; &lt;/div&gt; {% else %} &lt;p&gt;There are no objects.&lt;/p&gt; {% endif %} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload