我想爲表單無效的情況下分配一個值。這就是我正在做的。然而,如果表格無效,我的回覆很奇怪,我也沒有表格。form_invalid FormView設置上下文
{'key': 'Val', 'form': <MainLoginForm bound=True, valid=True, fields=(user_name;user_category;user_password)>, u'view': <mainApp.views.MainLoginFormView object at 0x107553c10>} Submit
這是類
class MainLoginFormView(FormView):
template_name = 'login.html'
form_class = MainLoginForm
success_url = "login.hrml"
args = {}
def ValidateAccount(self,form):
if(valid) #Some Condition to confirm if valid form
return super(MainLoginFormView, self).form_valid(form)
else:
return self.form_invalid(form)
def form_invalid(self, form):
MainLoginFormView.args = super(MainLoginFormView, self).get_context_data(**MainLoginFormView.args)
MainLoginFormView.args["key"] = "Val"
MainLoginFormView.args["form"] = form
#return self.render_to_response(context=MainLoginFormView.args)
return super(MainLoginFormView,self).form_invalid(MainLoginFormView.args)
def form_valid(self, form, **kwargs):
MainLoginFormView.args = kwargs
.....
return self.ValidateAccount(form)