前提・実現したいこと
ここに質問の内容を詳しく書いてください。
PythonのFlaskを使っていいね機能の実装が完了しました。
次はajaxを使って非同期通信をしたいのですが、どのように実装するのかアドバイスをいただけないでしょうか。
###つまづいている点
こちらなどを参考にしてみたのですが、
https://qiita.com/uuuno/items/5cf1e994dba95453f3ae
・blueprintを使用している際のURL指定。
・routeのint:blog_post_idと<action>部分をどのようにajax通信でデータを持って来れば良いのか分からずつまづいています。
該当のソースコード
python
1 2 3 4view.py 5 6@blog_posts.route('/like/<int:blog_post_id>/<action>') 7@login_required 8 9def like_action(blog_post_id,action): 10 blog_post = BlogPost.query.filter_by(id=blog_post_id).first_or_404() 11 if action == 'like': 12 current_user.like_post(blog_post) 13 db.session.commit() 14 15 if action == 'unlike': 16 current_user.unlike_post(blog_post) 17 db.session.commit() 18 return redirect(request.referrer) 19 20
model.py class User(db.Model,UserMixin): __tablename__ = 'users' id = db.Column(db.Integer,primary_key=True) profile_image = db.Column(db.String(64),nullable=False,default='default_profile.png') email = db.Column(db.String(64),unique=True,index=True) username = db.Column(db.String(64),unique=True,index=True) info = db.Column(db.String(250)) last_seen = db.Column(db.DateTime,default=datetime.utcnow) password_hash = db.Column(db.String(128)) posts = db.relationship('BlogPost',backref='author',lazy=True) comments = db.relationship('Comment',backref='poster',lazy=True) followed = db.relationship( 'User',secondary=followers, primaryjoin=(followers.c.follower_id == id), secondaryjoin=(followers.c.followed_id == id), backref=db.backref('followers',lazy='dynamic'),lazy='dynamic') liked = db.relationship( 'PostLike', foreign_keys='PostLike.user_id', backref='user',lazy='dynamic') def __init__(self,email,username,password): self.email = email self.username = username self.password_hash = generate_password_hash(password) def check_password(self,password): return check_password_hash(self.password_hash,password) def __repr__(self): return f"Username {self.username}" def like_post(self,blog_post): if not self.has_liked_post(blog_post): like = PostLike(user_id=self.id,post_id=blog_post.id) db.session.add(like) def unlike_post(self,blog_post): if self.has_liked_post(blog_post): PostLike.query.filter_by( user_id=self.id, post_id=blog_post.id).delete() def has_liked_post(self,blog_post): return PostLike.query.filter( PostLike.user_id == self.id, PostLike.post_id == blog_post.id).count() > 0
index.py {% for post in blog_posts.items %} {% if current_user.is_authenticated %} {% if current_user.has_liked_post(post) %} <a href="{{ url_for('core.like_action', blog_post_id=post.id, action='unlike') }}" style="text-decoration: none; color:black;"><i style="color:red;" class="fas fa-heart"></i></a> {% else %} <a href="{{ url_for('core.like_action', blog_post_id=post.id, action='like') }}" style="text-decoration: none; color:black;"><i style="color:red;" class="far fa-heart"></i></a> {% endif %} {{ post.likes.count()}} {% else %} <i class="fas fa-heart" style="color:red; font-size:25px;"></i>{{ post.likes.count()}} {% endif %} {% endfor %}
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー