这个程序是基于Flask的,它允许用户点击“LIKE”按钮来增加帖子的LIKE\u计数。你知道吗
使用下面的命令,我无法将postid发布到“/like”函数,也无法从中获取回调。终端显示类型错误:
like() missing 1 required positional argument: 'postid'.
<a href="#" onclick="like(this, {{ post.post_id }});">LIKE({{ post.like_count }})</a>
<script type="text/javascript">
function like(doc, postid){
$.ajax({
url:'{{ url_for('main.like') }}',
data:{postid:postid},
type:'POST',
success:function(callback){
var temp = 'LIKE' + callback;
$(doc).text(temp)
}
});
}
</script>
@main.route('/like', methods=['POST'])
@login_required
def like(postid):
post = Post.query.filter_by(post_id=postid).first()
new_count = post.like_count + 1
post.like_count = new_count +1
db.session.add(post)
db.session.commit()
return Response(new_count)
您将
postid
发布为表单数据,而不是url值。从视图签名中删除参数并从request.form
中获取值。你知道吗相关问题 更多 >
编程相关推荐