我在django 1.8中有一个项目,我想提取date_start
字段以获得一年,然后在事件发生的那一年创建一个范围。然后将其全部传递给SelectDateWidget
格式:
date_start = forms.DateField(widget=SelectDateWidget(years=range(1980, 2018)))
但我收了:too many values to unpack (expected 2)
这是我的代码中有问题的部分:
context['years'] = models.Booking.objects.filter('date_start')
以下是我视图中的所有代码:
class BookingListView(ListView, FormView):
model = models.Booking
form_class = BookingForm
queryset = models.Booking.objects.order_by('-date_start')
paginate_by = 80
template_name = 'events/archive_list.html'
context_object_name = 'object_list'
date_field = 'date_start'
allow_future = True
def get_context_data(self, **kwargs):
context = super(BookingListView, self).get_context_data(**kwargs)
context['mode'] = 'archive'
context['form'] = BookingForm()
context['years'] = models.Booking.objects.filter('date_start')
print(context['years'])
return context
答案 0 :(得分:1)
您使用的是字符串"date_start"
,而不是变量date_start
。将您的代码更改为此代码,它应该可以正常工作。
context['years'] = models.Booking.objects.filter(date_start)