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

Unparseable date with SimpleXml

java android simple-framework

I am using Simple XML for XML serialization in my Android project. I have problem with parsing a Date object. I receive an exception:

Word to html conversion using php

php html ms-word

I am working on a linux server.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.