为什么这不起作用而是返回ValueError
?我正在尝试从用户那里获得输入,然后根据用户的选择,呈现特定视图。如果我将return redirect()
移回post function
内,它确实有效。
class Choice(View):
form_class = SomeForm
template_name = 'app/object_form.html'
def get(self,request,*args, **kwargs):
form = self.form_class()
return render (request, self.template_name, {'form':form})
def post(self,request,*args,**kwargs):
form = self.form_class(request.POST)
if form.is_valid():
self.redirect_to_createform(request,form.cleaned_data['choice'])
else:
print('form not valid')
print(form.errors)
return render (request, self.template_name, {'form':form})
def redirect_to_createform(self, request, option):
print(option)
## Here i should have some logic to redirect to different views depending on the `option`
return redirect('to_somewhere')
编辑 - 我理解错误的含义,但我如何实现上述目标。
答案 0 :(得分:4)
您缺少返回声明,如下所示
if form.is_valid():
return self.redirect_to_createform(request,form.cleaned_data['choice'])