2017-01-20 7 views
-1

FormにFlaskでWTFormsモジュールを作成しようとしましたが、フォームに使用する変数を初期化するためにコンストラクタを作成する必要があります。wtformsでFlaskを実装する

コードは次である:

startup.py

@app.route("/startup/new", methods=["GET"]) 
def formNewStartUp(): 

    newForm = NewStartUpForm(request.form) 

    return render_template("platform/startup/new.html", newForm=newForm.getForm()) 

newStartUpForm.py

class NewStartUpForm(Form): 

    # Constructor 
    def __init__(self, *arg, **kwarg): 
     self.aCategories = StartupCategories() # Another class 
     self.lang = getUserLanguage(request) # Language 

    def getForm(self, *arg, **kwarg): 

     # Detail Main 
     titleStartup = TextField() 
     webStartup = TextField() 
     groupStartUp = SelectField('Groups') 
     categoryStartUp = SelectField('Categories', choices=self.aCategories.getAllCategoriesByLang(self.lang)) 
     shortDescription = TextAreaField() 

Iロードする "をgetForm()" 関数を呼び出すオブジェクトを初期化した後フォームを使用していますが、私がHTML側にいる場合、出力は「なし」です。

何が悪いですか?

答えて

1

get_form()メソッドは何も返さないので正常ではありません。下記のようなものがあなたのために働くはずです:

class NewStartUpForm(Form): 
    def __init__(self, *arg, **kwarg): 
     self.aCategories = StartupCategories() 
     self.lang = getUserLanguage(request) 
    def getForm(self, *arg, **kwarg): 
     choices=self.aCategories.getAllCategoriesByLang(self.lang) 
     return SecondForm(choices) 

class SecondForm(Form): 
    titleStartup = TextField() 
    webStartup = TextField() 
    groupStartUp = SelectField('Groups') 
    categoryStartUp = SelectField('Categories') 
    shortDescription = TextAreaField() 
    def __init__(self, choices, *args, **kwargs): 
     super(SecondForm, self).__init__(*args, **kwargs) 
     self.categoryStartUp.choices = choices 
関連する問題