【发布时间】:2014-03-25 16:38:54
【问题描述】:
我似乎无法找到正确的行来使用我在同一 html 页面上的 WTforms 上创建的 2 个表单:
我定义了这两种形式
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")
我想在我的 Dashboard.html 页面中显示它们,我在该页面中嵌入了代码以在 2 个不同的表单块中显示它们。
然后我尝试为我的 dashboard.html 创建视图函数,在其中调用/定义 2 个表单
@app.route('/dashboard.html')
def dashboard():
form = Area()
M_form = Message()
return render_template('dashboard.html',form= Area(),M_form = Message())
但我得到了一个
M_form = Message()
NameError: global name 'Message' is not defined
我想这一定是某种基本的结构性错误,也许我无法在我的视图中定义 2 个表单,但是我如何在不必求助于 2 个不同的 html 页面(每个表单一个)的情况下进行编码?
【问题讨论】:
-
Message类是否已导入您定义路线的位置? -
检查你的文件中是否导入了消息类
-
谢谢,我猜这很简单。我不知道为什么,但我在想所有存储在我的 forms.py 中的类(我保存表单的地方)已经从 Forms 导入进口
-
(完成我上面的评论)我在想所有的类都是从`from from import`导入的,但是你也需要导入每个类。我真的没有关注那部分,一直认为这是 WTforms 中的一些微妙之处。无论如何,现在它可以工作了。非常感谢:)
标签: python flask wtforms flask-wtforms