简体   繁体   中英

flask wtforms multiple forms on the same html page

I can't seem to get the right lines to use 2 forms I created on WTforms on the same html page:

I define these 2 forms

class Area(Form):
    title = TextField("Title", [validators.Required("Please enter an Area Title")])
    text = TextAreaField("Text (max 50 characters)",[validators.Required("Please enter a Push text"),validators.Length(max=50,message="Area text cannot be more than 50 characters")])

class Message(Form):
    Message_title = TextField("Title", [validators.Required("Please enter a Message Title")])
    Message_Date_and_Time = DateTimeField("Date and Time")

and I want to display them in my Dashboard.html page where I embedded the code to display them within 2 different form blocks.

then I try to create view function for my dashboard.html where I call/define the 2 forms

@app.route('/dashboard.html')
def dashboard():
form = Area()
M_form = Message()
return render_template('dashboard.html',form= Area(),M_form = Message())

but I get a

    M_form = Message()    
NameError: global name 'Message' is not defined

I guess it must be some kind of basic, structural error, maybe I can t define 2 forms in my view but then how can i code this without having to resort to 2 different html pages (one for each form)?

from forms import Area, Message在脚本顶部执行from forms import Area, Message

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM