views.py
class PlayerListView(PlayerViewMixin, FormMixin, ListView):
form_class = PlayerFilterForm(initial={'min_rating': 1})
paginate_by = 50
template_name = 'players/player_list.html'
def get_context_data(self, **kwargs):
context = super(PlayerListView, self).get_context_data()
context['form'] = self.form_class
# context['form'].fields['maxrating'].initial = self.request.GET.get('maxrating')
print(self.get_initial())
return context
def get_queryset(self):
qs = super(PlayerListView, self).get_queryset()
minrating = self.request.GET.get('minrating')
maxrating = self.request.GET.get('maxrating')
if self.request.GET.get('minrating'):
qs = qs.filter(overall_rating__gte=minrating)
if self.request.GET.get('maxrating'):
qs = qs.filter(overall_rating__lte=maxrating)
return qs
forms.py
class PlayerFilterForm(forms.Form):
min_rating = forms.ChoiceField(
initial=1,
choices=[
(x, x) for x in range(1, 100)
]
)
...
代码很混乱,此刻只是玩弄。我需要以一种方式来处理我可以在get_initial
中执行的动态初始数据,但无论我做什么,我的初始字典总是空的。我错过了一些非常愚蠢的东西吗?