在我的 Django 应用程序中,我有一个ModelChoiceField
用于选择对象的索引视图。当我单击提交按钮时,我想将该对象传递给下一个视图,但它不起作用。
这是我的代码: 意见:
def index(request):
args = {}
args.update(csrf(request))
if request.user.is_authenticated():
if request.POST:
form = EquipmentForm(request.POST)
if form.is_valid():
form.save()
request.session['chosen_equipment'] = form.cleaner(self)
return HttpResponseRedirect('/lru/reservation/')
else:
form = EquipmentForm()
args['form'] = form
args['user'] = request.user
args['reservation_list'] = Reservation.objects.all()
return render_to_response('lru/index.html', args)
else:
return HttpResponseRedirect('/lru/login/')
def reservation(request):
if request.POST:
form = ReservationForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/lru/')
else:
form = ReservationForm()
form = ReservationForm(initial={'reserved_by': request.user})
args = {}
args.update(csrf(request))
args['form'] = form
args['chosen_equipment'] = request.session.get('chosen_equipment', ' Not found')
return render_to_response('lru/reservation.html', args)
形式:
class EquipmentForm(forms.Form):
class Meta:
model = Equipment
equipment = forms.ModelChoiceField(queryset=Equipment.objects.all())
def cleaner(self):
data = self.cleaned_data[equipment]
return data
我在这里做错了什么?