Paginate Django formset

前端 未结 5 1069
失恋的感觉
失恋的感觉 2020-12-29 14:42

I have a model formset that I want to display 10 forms at a time using Django\'s Paginator, but it can\'t be done like paginator = Paginator(formset, 10). What\

5条回答
  •  被撕碎了的回忆
    2020-12-29 15:23

    This is a generic example of the solution I found to my problem:

    In the forms.py file:

    class MyForm(ModelForm):
        class Meta:
            model = MyModel
            fields = ('description',)
    

    In the views.py file:

    from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
    
    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))
    

    You need to create the formset with the objects in the present page, otherwise, when you try to do formset = FormSet(request.POST, request.FILES) in the POST method, Django raises a MultiValueDictKeyError error.

    In the template.html file:

    {% if objects %}
        
    {% csrf_token %} {{ formset.management_form }} {% for form in formset.forms %} {{ form.id }} {{ form.as_p }} {% endfor %}
    {% else %}

    There are no objects.

    {% endif %}

提交回复
热议问题