出于某种原因,我无法像过去那样填充表单的字段。以下是相关位:
视图.py
def selectEvent(request, user=None):
if not user:
userID = request.user.id
if request.method == 'POST':
qform = eventForm(request.POST, user=userID)
if qform.is_valid():
qryData = qform.cleaned_data
event = qryData['event']
qurl = '/showGG/getWedGuest/%s' % (event)
return HttpResponseRedirect(qurl)
else:
qform = eventForm(user=userID)
return render(request, 'showGG/getEvent.html', {'form': qform})
表格.py
class eventForm(forms.Form):
def __init__(self, *args, **kwargs):
user=kwargs.pop('user')
super(eventForm, self).__init__(*args, **kwargs)
elist = [(e,e.name) for e in Event.objects.filter(organizer_id__exact=user)]
print 'eventForm: elist=',elist
event = forms.ChoiceField(choices=elist)
我需要复杂的eventForm.__init__()
因为我正在向它传递一个变量,但打印行显示它正在生成我期望的列表:
eventForm: elist= [(<Event: cd2 - 2013-08-01 00:17:39+00:00>, u'cd2')]
但是当我返回qform
返回时views.selectEvent
,它fields
和base_fields
变量都是空字典?为什么会这样?