我构建了一个包含两个字段的FormView。我想使用表单将查询传递给视图。
class SectorForm(forms.Form):
date = forms.DateField()
days = forms.IntegerField(max_value=365, min_value=1)
有没有办法可以从有效的表单字段中获取FormView并构建 success_url ?
重写get_success_url。
def get_success_url(self):
return reverse_lazy('queryable-view' date days)
我以为我可以从form_valid中取出它但从未取得过成功。
答案 0 :(得分:2)
get_success_url
无法访问Form
个实例,但form_valid
可以访问from django.shortcuts import redirect
class YourFormView(FormView):
# ...
def form_valid(self, form):
date, days = form.cleaned_data['date'], form.cleaned_data['days']
return redirect('queryable-view', date, days)
个实例。尝试这样的事情:
{{1}}
答案 1 :(得分:0)
我曾遇到过类似的问题。当您使用表单时,您可能不会使用kwargs并且get_success_url无法访问表单(如前所述) 我已经到处了:
class YourView(CreateView):
appointment_id =0
def form_valid(self,form):
appointment = form.save()
self.appointment_id = appointment.pk
print self.appointment_id
return super(AppointmentCreate,self).form_valid(form)
def get_success_url(self, **kwargs):
if kwargs != None:
return reverse_lazy('payment', kwargs = {'appid': self.appointment_id})