【发布时间】:2023-04-06 03:50:01
【问题描述】:
我正在尝试制作一个类似于迷你推文的网络应用程序。这些帖子是从数据库中提取的,我希望每个帖子都有一个“赞成投票”按钮,如下图所示。
每个帖子都有一个id、author、body 和likes 属性。当点击赞成票时,likes 属性需要更新。
我的问题是如何确定点击了哪个按钮。在这种情况下,route() 函数和 html 模板有什么好的策略?
我正在考虑为每个按钮添加一个名称并将post.id 放入名称中,然后检查request 是否有它。但是发帖数事先不知道,request签入route()函数应该怎么写?
我目前的模板如下
<table class="table table-striped">
{% for post in posts %}
<tr>
<td> {{ post.id }} </td>
<td> <img src="{{ post.author.avatar(50) }}"> </td>
<td> <b>{{ post.body }}</b> </td>
<td> <button type="button" name='{{'up.'+ post.id|string}}' class="btn btn-default">
<span class="glyphicon glyphicon-thumbs-up" aria-hidden="true"></span>
</button>
{{ post.likes}} </td>
</tr>
{% endfor %}
</table>
而现在的route()是这样的
@bbs.route('/', methods=['GET', 'POST'])
def index():
posts = Post.query.all()
return render_template('bbs/index.html', posts=posts)
【问题讨论】:
标签: python html flask forms http-post