Как сделать лайки на Flask?
У меня есть новостной сайт на flask, и я хочу сделать систему лайков в нём, но не знаю как.
Вот мой код:
@bp.route('/article/<int:id>', methods=['GET', 'POST'])
def article(id):
comment_form = CommentsCreateForm()
like_form = LikesAndDislikes()
article = db.session.query(Articles).filter(Articles.id == id).first()
creator = db.session.query(User).filter(User.id == article.creator).first()
comments = db.session.query(Comment).filter(Comment.article_id == id).all() # список кометариев
if comment_form.validate_on_submit():
comment = Comment()
comment.comment_creator = current_user.id
comment.article_id = id
comment.content = comment_form.text.data
current_user.comment.append(comment)
db.session.commit()
return render_template("news_template.html", article=article, creator=creator, comments=comments,
form=comment_form)
return render_template("news_template.html", article=article, creator=creator, comments=comments, form=comment_form)
Форма для создания комментария
class CommentsCreateForm(FlaskForm):
text = StringField('Введите коментарий', validators=[DataRequired()])
submit = SubmitField('Отправить')
{% extends "base.html" %}
{% block content %}
<div class="card text-center">
<img src="/static/img/{{article.image}}">
<div class="card-body text-left">
<h5 class="card-title">{{ article.title }}</h5>
<p class="card-text">{{article.content}}</p>
<p>Автор: {{creator.username}}</p>
</div>
<div class="card-footer text-left">
<button >
<svg class="bi bi-caret-up" width="1em" height="1em" viewBox="0 0 16 16" fill="Green"
xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd"
d="M3.204 11L8 5.519 12.796 11H3.204zm-.753-.659l4.796-5.48a1 1 0 011.506 0l4.796 5.48c.566.647.106 1.659-.753 1.659H3.204a1 1 0 01-.753-1.659z"
clip-rule="evenodd"/>
</svg>
</button>
{{article.likes}}
<svg class="bi bi-caret-down" width="1em" height="1em" viewBox="0 0 16 16" fill="Red"
xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd"
d="M3.204 5L8 10.481 12.796 5H3.204zm-.753.659l4.796 5.48a1 1 0 001.506 0l4.796-5.48c.566-.647.106-1.659-.753-1.659H3.204a1 1 0 00-.753 1.659z"
clip-rule="evenodd"/>
</svg>
{{article.dislikes}}
</div>
</div>
{% if current_user.is_authenticated %}
<form action="" method="post" enctype="multipart/form-data">
{{ form.hidden_tag() }}
{{ form.csrf_token }}
<div class="form-group row">
{{ form.text.label(class="col-sm-2 col-form-label col-form-label") }}
<div class="col-sm-10">
{{ form.text(type="email", class="form-control", id="colFormLabel", placeholder="col-form-label") }}
</div>
{% for error in form.text.errors %}
<p class="alert alert-danger" role="alert">
{{ error }}
</p>
{% endfor %}
</div>
<p>{{ form.submit(type="submit", class="btn btn-primary") }}</p>
</form>
{% endif %}
<h3>Коментарии</h3>
<div class="list-group">
{% for comment in comments %}
<a class="list-group-item list-group-item-action">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1">{{ comment.comment_creator }}</h5>
<small>{{comment.created_date}}</small>
</div>
<p class="mb-1">{{comment.content}}</p>
</a>
{% endfor %}
</div>
{% endblock %}