flaskform pass a variable (WTForms)

不羁岁月 提交于 2019-12-11 15:24:53

问题


I want to pass a str to be used as the prompt for a form. I thought it would be simple but it is proving to be difficult.

Here is my code:

class PostForm(FlaskForm):
    post = TextAreaField(Question, validators=[DataRequired()])
    submit = SubmitField('Submit')`

And,

form = PostForm('my question')

the corresponding html

{{ wtf.quick_form(form) }}

回答1:


So, I still don't have an answer to the question, but I did manage to come up with a solution.

class PostForm(FlaskForm):
    post = TextAreaField(_l('This no longer matters'), validators=[DataRequired()])
    submit = SubmitField(_l('Submit'))

And then in the routes

from wtforms.fields.core import Label
form = PostForm()
form.post.label = Label("post", 'Real question goes here')}

The explanation for this is that TextAreaField creates a label attribute on post that is accessible and changable, but it needs to be formated correctly as a Label object from wtforms.fields.core. (Simply reassigning it as a string did not work). The representation of this object is:

<label for="post">Real question goes here</label>

And it is of type

<class 'wtforms.fields.core.Label'>


来源:https://stackoverflow.com/questions/51181324/flaskform-pass-a-variable-wtforms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!