【发布时间】:2021-11-03 20:35:55
【问题描述】:
我正在我的网站上开发一个通知系统。我决定在数据库中为他们制作一个模型。我还在 views.py 中创建了一个函数,该函数接受接收通知的用户的参数和通知的类型。代码如下:
@views.route('/send_notification',methods = ['GET','POST'])
@login_required
def send_notification(user, type):
reciever = User.query.filter_by(username = user).first()
if type == 'family-invitation':
family = Family.query.filter_by(id = current_user.family_id).first()
description = "invites you to their family!"
new_notification = Notification(sender = current_user.username, sender_prof_img = family.prof_img, user_id = reciever.id, description = description)
db.session.add(new_notification)
db.session.commit()
flash("Invitation to family has been sent!", category="success")
return True
现在我希望能够在有人邀请他们时向用户发送通知。我正在考虑当有人按下按钮时调用 python 函数,但我不知道如何编码。
我想出了类似的方法,但它不起作用:
<button
onclick="window.location.href = '{{ url_for('views.send_notification(profileUser.username, 'family-invitation')') }}';"
>
Invite to family
</button>
【问题讨论】:
标签: python html flask flask-sqlalchemy