Django form submit error

I'm using kwargs to populate my djnago form initially,

  • Forms.py

     class myForm(Forms.Form):  
       textbox1 = forms.CharField()  
       dropdown1 = forms.ChoiceField()  
    
    
       def __init__(self,*args,**kwargs):  
         choices = kwargs.pop('choices')  
         super(myForm,self).__init__(*args,**kwargs)  
         self.fields['dropdown1'] = forms.ChoiceField(choices=choices)  
    
  • views.py

    def myjob(req):  
      ch = {("1","A"),("2","B")}  
      if req.method == "POST":  
        frm = myForm(data=req.POST) # throws error saying can't find choices in Forms.py  
      else:  
        frm = myForm(choices=ch) # no problem here  
    

This code is throwing an error while submitting data as it is trying to call init again. How can I submit correctly?

Answers


The error seems quite clear: the code is expecting a choices kwarg, and you're not providing it on POST. You can simply add it:

if req.method == "POST":  
    frm = myForm(data=req.POST, choices=ch)

Need Your Help

Pass object through Link in react router

reactjs reactjs-flux react-router

Is it possible to pass an object via Link component in react-router?