如何在Python Flask中实现类似按钮的功能? [英] How do i implement a like button function to posts in Python Flask?
问题描述
因此,我试图在我的代码中添加一个喜欢按钮的功能,该功能允许用户喜欢特定帖子.点赞次数将链接到已登录的用户,并显示点赞次数.实施前端不会很困难,但是后端存在问题.
So i am trying to add to add a like button feature to my code that allows users to like specific posts. The likes will be linked to a logged in user and the number of likes will be shown. Implementing the front end wont be difficult but i am having a problem with the back end.
这是我到目前为止所拥有的?
This is what I have so far?
我在models.py中为喜欢创建了一个表:
I have created a table for likes in models.py :
likers = db.Table('likers',
db.Column('liker_id', db.Integer, db.ForeignKey('post.id')),
db.Column('liked_id', db.Integer, db.ForeignKey('post.id'))
)
在我的用户类的Models.py中:
In my Models.py for my user class:
class User(db.Model, UserMixin):
#Code
liked = db.relationship(
'User', secondary=likers,
primaryjoin=(likers.c.liker_id == id),
secondaryjoin=(likers.c.liked_id == id),
backref = db.backref('likers', lazy='dynamic'), lazy='dynamic')
def like(self, post):
if not self.is_liking(post):
self.liked.append(post)
def unlike(self, post):
if self.is_liking(post):
self.liked.remove(post)
def is_liking(self, post):
return self.liked.filter(
likers.c.liked_id == post.id).count() > 0
在routes.py中-对于我的用户蓝图,我有:
in my routes.py- for my users blueprint I have:
@users.route("/like/<int:post_id>")
@login_required
def like(post_id):
post = Post.query.get_or_404(post_id)
current_user.like(post)
db.session.commit()
flash('Post has been liked')
return redirect(url_for('posts.post', post_id=post.id))
@users.route("/unlike/<int:post_id>")
@login_required
def unlike(post_id):
post = Post.query.get_or_404(post_id)
current_user.unlike(post)
db.session.commit()
flash('Post has been unliked')
return redirect(url_for('posts.post', post_id=post.id))
我在做什么错?我不断收到诸如:
What am i doing wrong? I keep getting errors such as :
builtins.KeyError
KeyError: 'likers'
我已经完成了一个评论部分,我知道喜欢的人会比评论更贴切,但是我正在努力实现它.我是不熟悉Flask的人,我尝试使用这些文档,但找不到任何可帮助我的东西...
I have done a comment section annd i know the relatioship for the likes will be simialr to the comments but i am struggling to implement it. I am relatively new to flask and i have tried using the documentations but haven't found anything to help me...
这是我最后的希望.
推荐答案
class User(UserMixin, db.Model):
# Code
liked = db.relationship(
'PostLike',
foreign_keys='PostLike.user_id',
backref='user', lazy='dynamic')
def like_post(self, post):
if not self.has_liked_post(post):
like = PostLike(user_id=self.id, post_id=post.id)
db.session.add(like)
def unlike_post(self, post):
if self.has_liked_post(post):
PostLike.query.filter_by(
user_id=self.id,
post_id=post.id).delete()
def has_liked_post(self, post):
return PostLike.query.filter(
PostLike.user_id == self.id,
PostLike.post_id == post.id).count() > 0
class PostLike(db.Model):
__tablename__ = 'post_like'
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
post_id = db.Column(db.Integer, db.ForeignKey('post.id'))
@app.route('/like/<int:post_id>/<action>')
@login_required
def like_action(post_id, action):
post = Post.query.filter_by(id=post_id).first_or_404()
if action == 'like':
current_user.like_post(post)
db.session.commit()
if action == 'unlike':
current_user.unlike_post(post)
db.session.commit()
return redirect(request.referrer)
然后,当您列出自己的帖子时,设置锚点如下:
Then when you're listing your posts, set your anchors something like this:
{% for post in posts %}
{% if current_user.has_liked_post(post) %}
<a href="{{ url_for('like_action', post_id=post.id, action='unlike') }}">Unlike</a>
{% else %}
<a href="{{ url_for('like_action', post_id=post.id, action='like') }}">Like</a>
{% endif %}
{% endfor %}
让我们假设您的Post
模型看起来像这样:
Let's assume your Post
model looks something like this:
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
body = db.Column(db.Text)
author_id = db.Column(db.Integer, db.ForeignKey('user.id'))
recipient_id = db.Column(db.Integer, db.ForeignKey('user.id'))
likes = db.relationship('PostLike', backref='post', lazy='dynamic')
您将使用:
p = Post.query.filter_by(id=1).first()
p.likes.count()
或者,您可以在.html文件中使用它:
Or, you'd use this in your .html file:
{% for post in posts %}
{% if current_user.has_liked_post(post) %}
<a href="{{ url_for('like_action', post_id=post.id, action='unlike') }}">Unlike</a>
{% else %}
<a href="{{ url_for('like_action', post_id=post.id, action='like') }}">Like</a>
{% endif %}
{{ post.likes.count() }} likes
{% endfor %}
这篇关于如何在Python Flask中实现类似按钮的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!