Using django-tables2 to paginate in a FormView

345 views
Skip to first unread message

Frankline

unread,
Jun 18, 2014, 1:36:31 AM6/18/14
to django...@googlegroups.com

I am using django-tables2 to paginate my objects, but I am unable to paginate it. See my code below:

forms.py

import django_tables2 as tables
from django.forms.widgets import CheckboxSelectMultiple
from django.forms.models import ModelMultipleChoiceField
from django.forms import Form
from django.utils.translation import ugettext_lazy as _
from my.apps.enrolment.models import Enrolment
from .models import SmartCard

class CreateSmartCardsTable(tables.Table):
    pk = tables.CheckBoxColumn(tables.Attrs(td__input={'name': 'enrolments'}))
    location = tables.Column(_('location'), accessor='household.location')
    family_name = tables.Column(_('family name'), accessor='household.name')
    recipient_name = tables.Column(_('recipient name'), accessor='person')
    number_of_beneficiaries = tables.Column(_('number of beneficiaries'), accessor='beneficiaries.count')

class CreateSmartCardsSelectMultiple(CheckboxSelectMultiple):
    def render(self, name, value, attrs=None, choices=Enrolment.objects.none()):
        table = CreateSmartCardsTable(self.choices)
        # The code below is where I want to paginate, but cannot get hold of the request object
        #table.paginate(page=request.GET.get('page', 1), per_page=25)
        return table.as_html()

class CreateSmartCardsMultipleChoiceField(ModelMultipleChoiceField):
    widget = CreateSmartCardsSelectMultiple

    @property
    def choices(self):
        return self.queryset

class CreateSmartCardsForm(Form):
    enrolments = CreateSmartCardsMultipleChoiceField(queryset=Enrolment.objects.none(), required=False, label='')

    def __init__(self, *args, **kwargs):
        super(CreateSmartCardsForm, self).__init__(*args, **kwargs)
        self.enrolments = Enrolment.objects.approved()
        self.fields['enrolments'].queryset = self.enrolments

views.py

from logging import getLogger
from django.core.urlresolvers import reverse
from django.utils.translation import ungettext
from django.views.generic import FormView
from my.apps.enrolment.models import Enrolment
from my.apps.smartcard.forms import CreateSmartCardsForm

class CreateSmartCardsView(SelectedInterventionMixin, FormView):
    """Create smart cards of enrolments."""
    template_name = 'smartcard/create_smart_cards.html'
    form_class = CreateSmartCardsForm
    permissions = ['smartcard.create_smart_card']

    def get_form_kwargs(self):
        kwargs = super(CreateSmartCardsView, self).get_form_kwargs()
        kwargs['intervention'] = self.selected_intervention
        return kwargs

    def form_valid(self, form):
        # My form_valid logic goes here
        return super(CreateSmartCardsView, self).form_valid(form)

    def get_success_url(self):
        return reverse('smartcard.confirm_printed', args=self.some_obj.get_url_args())

Somewhere along the line that includes table.paginate(page=request.GET.get('page', 1), per_page=25) is where I want to paginate. But I am unable to get a hold of the request object to enable the pagination.

How can I achieve this?

Reply all
Reply to author
Forward
0 new messages