Flask wtforms - 'UnboundField' object is not callable, dynamic field won't init properly

后端 未结 1 649
借酒劲吻你
借酒劲吻你 2021-01-20 01:42

app.py

from flask import Flask, render_template
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, FieldList, FormField

app = Fla         


        
1条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-20 02:28

    It's not the ideal solution, but this works though.

    from flask import Flask, render_template
    from flask_wtf import FlaskForm, Form
    from wtforms import StringField, SubmitField, FieldList, FormField
    
    app = Flask(__name__)
    app.config['SECRET_KEY'] = 'apple pie'
    
    
    class BookForm(FlaskForm):
        book = StringField('book title')
    
    
    class LibraryForm(FlaskForm):
        library = StringField('Library name')
        books = FieldList(FormField(BookForm))
        submit = SubmitField('Submit')
    
    
    @app.route('/book', methods=['GET', 'POST'])
    def book():
        form = LibraryForm()
        if form.validate_on_submit():
            return 'aww yeah'
        for i in range(6):
            form.books.append_entry()
    
        return render_template('books.html', form = form)
    

    0 讨论(0)
提交回复
热议问题