我正在使用django-tables2
(请参阅here)对我的对象进行分页,但我无法对其进行分页。
请参阅下面的代码:
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())
包括在线的某个地方
table.paginate(page=request.GET.get('page', 1), per_page=25)
是我要分页的地方。但我无法获取请求对象以启用分页。
我怎样才能做到这一点?