2017-01-16 15 views
0

フォームが無効な場合、コンテキストに値を割り当てようとしています。これは私がやっていることです。しかし、フォームを無効にすると私の応答が変わってしまい、私はフォームを取得しません。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) 

答えて

0

form_invalid方法は、入力としてのみ形態をとるです。あなたはこのようなあなたの要件を満たすために、それを上書きすることができます。

あなたはそれがつもりだ

def form_invalid(self, form): 
    # Do your your stuff here , 
    MainLoginFormView.kargs["key"] = "Val" 
    MainLoginFormView.kargs["form"] = form 
    return self.render_to_response(self.get_context_data(**MainLoginFormView.kargs)) 
0

をkwargsからする引数の名前を変更する必要があり、少し奇妙なようだが、更新の責任に応答コンテキストを委任することが推奨され方法はget_context_dataです。

def get_context_data(self, **kwargs): 
    form = kwargs.pop('form', None) # form becomes None if no form key is provided 
    ctx = super(MainLoginFormView, self).get_context_data(form=form) # This super call will append the form to our context 
    ctx.update(**kwargs) # Let's append the extra_args to our context 
    return ctx 

def form_invalid(self, form, **kwargs): 
    # in order to work, the super call to get_context_data needs a form item 
    kwargs.update({'form': form}) 
    return self.render_to_response(self.get_context_data(**kwargs)) 

def ValidateAccount(self, form): 
    if(valid) #Some Condition to confirm if valid form 
     return super(MainLoginFormView, self).form_valid(form) 
    else: 
     extra_args = { 
      "key": "Val" 
     } 
     return self.form_invalid(form, **extra_args) 

{'key': 'Val'}はValidateAccount方法で添付されています。

関連する問題