如何通过表单将信息从 html(jinja) 传递到 python(flask)
How to pass information from html(jinja) to python(flask) through a form
我正在尝试在 The Flask Mega-Tutorial 的博客中添加评论 posts 的可能性(使用新的 post 作为评论)。我的问题与数据库无关。我不知道如何 "link" 将表格 parent post,然后评论会引用他们的 parents.
这里有一些代码...
观点:
@app.route('/', methods=['GET', 'POST'])
@app.route('/index', methods=['GET', 'POST'])
@app.route('/index/<int:page>', methods=['GET', 'POST'])
@login_required
def index(page=1):
form = PostForm()
if form.validate_on_submit():
post = Post(body=form.post.data, timestamp=datetime.utcnow(),author=g.user)
db.session.add(post)
db.session.commit()
flash('Your post is now live!')
return redirect(url_for('index'))
posts = g.user.followed_posts().paginate(page, POSTS_PER_PAGE, False)
return render_template('index.html',
title='Home',
form=form,
posts=posts)
形式:
class PostForm(Form):
post = StringField('post', validators=[DataRequired()])
index.html的部分(post和表格在同一行...)
{% for post in posts.items %}
<table>
<tr valign="top">
<td><img src="{{ post.author.avatar(50) }}"></td><td><p><a href="{{ url_for('user', nickname=post.author.nickname)}}">{{ post.author.nickname }}</a> said {{ momentjs(post.timestamp).fromNow() }}:</p>
<p><strong>{{ post.body }}</strong></p></td>
</tr>
<tr>
<form action="" method="post" name="post">
{{form.hidden_tag() }}
<table>
<tr>
<td> <p>------------- </p> </td>
<td>{{ form.post(size=10, maxlength=140)}}</td>
<td>
{% for error in form.post.errors %}
<span> style="color: red;"[{{error}}] </span><br>
{% endfor %}
</td>
<td><input type="submit" value="Reply!"></td>
</table>
</form>
</tr>
</table>
{% endfor %}
以及 Post 的数据库模型:
class Post(db.Model):
__searchable__ = ['body']
id = db.Column(db.Integer, primary_key = True)
body = db.Column(db.String(140))
timestamp = db.Column(db.DateTime)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
parent_id = db.Column(db.Integer, db.ForeignKey('post.id'))
children = db.relationship("Post")
def __repr__(self):
return '<Post %r>' % (self.body)
我的想法是添加一个新的 post 作为 "comment",为其提供对旧 post (parent_id) 的数据库引用。我如何配置表单才能知道 parent(parent_id)是谁?我是否应该处理 index.html 并找到一种方法向表单提供有关 parent post 的信息?
非常感谢!
从技术上讲,您不应该在一个页面上有多个相同表单的实例,但是……您可以使用 HiddenField 来自动填写父级 post 的 ID。
在您的 PostForm class 中,添加一个 HiddenField(记得导入它:from wtforms import HiddenField
):
class PostForm(Form):
post = StringField('post', validators=[DataRequired()])
parentID = HiddenField('', validators=[DataRequired()])
在您的模板中,在 form
标签的任意位置添加:{{ field(value=post.id) }}
。
在您的验证方法中,将父 ID 添加到 post class 实例的创建中:
def index(page=1):
form = PostForm()
if form.validate_on_submit():
post = Post(body=form.post.data, parent_id=form.parentID.data, timestamp=datetime.utcnow(),author=g.user)
我正在尝试在 The Flask Mega-Tutorial 的博客中添加评论 posts 的可能性(使用新的 post 作为评论)。我的问题与数据库无关。我不知道如何 "link" 将表格 parent post,然后评论会引用他们的 parents.
这里有一些代码...
观点:
@app.route('/', methods=['GET', 'POST'])
@app.route('/index', methods=['GET', 'POST'])
@app.route('/index/<int:page>', methods=['GET', 'POST'])
@login_required
def index(page=1):
form = PostForm()
if form.validate_on_submit():
post = Post(body=form.post.data, timestamp=datetime.utcnow(),author=g.user)
db.session.add(post)
db.session.commit()
flash('Your post is now live!')
return redirect(url_for('index'))
posts = g.user.followed_posts().paginate(page, POSTS_PER_PAGE, False)
return render_template('index.html',
title='Home',
form=form,
posts=posts)
形式:
class PostForm(Form):
post = StringField('post', validators=[DataRequired()])
index.html的部分(post和表格在同一行...)
{% for post in posts.items %}
<table>
<tr valign="top">
<td><img src="{{ post.author.avatar(50) }}"></td><td><p><a href="{{ url_for('user', nickname=post.author.nickname)}}">{{ post.author.nickname }}</a> said {{ momentjs(post.timestamp).fromNow() }}:</p>
<p><strong>{{ post.body }}</strong></p></td>
</tr>
<tr>
<form action="" method="post" name="post">
{{form.hidden_tag() }}
<table>
<tr>
<td> <p>------------- </p> </td>
<td>{{ form.post(size=10, maxlength=140)}}</td>
<td>
{% for error in form.post.errors %}
<span> style="color: red;"[{{error}}] </span><br>
{% endfor %}
</td>
<td><input type="submit" value="Reply!"></td>
</table>
</form>
</tr>
</table>
{% endfor %}
以及 Post 的数据库模型:
class Post(db.Model):
__searchable__ = ['body']
id = db.Column(db.Integer, primary_key = True)
body = db.Column(db.String(140))
timestamp = db.Column(db.DateTime)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
parent_id = db.Column(db.Integer, db.ForeignKey('post.id'))
children = db.relationship("Post")
def __repr__(self):
return '<Post %r>' % (self.body)
我的想法是添加一个新的 post 作为 "comment",为其提供对旧 post (parent_id) 的数据库引用。我如何配置表单才能知道 parent(parent_id)是谁?我是否应该处理 index.html 并找到一种方法向表单提供有关 parent post 的信息?
非常感谢!
从技术上讲,您不应该在一个页面上有多个相同表单的实例,但是……您可以使用 HiddenField 来自动填写父级 post 的 ID。
在您的 PostForm class 中,添加一个 HiddenField(记得导入它:from wtforms import HiddenField
):
class PostForm(Form):
post = StringField('post', validators=[DataRequired()])
parentID = HiddenField('', validators=[DataRequired()])
在您的模板中,在 form
标签的任意位置添加:{{ field(value=post.id) }}
。
在您的验证方法中,将父 ID 添加到 post class 实例的创建中:
def index(page=1):
form = PostForm()
if form.validate_on_submit():
post = Post(body=form.post.data, parent_id=form.parentID.data, timestamp=datetime.utcnow(),author=g.user)