Answer the question
In order to leave comments, you need to log in
Flask, why do I get a 404 error when submitting a request?
Here is a view that shows the entry (verse) and contains the form for submitting a comment:
@app.route('/comment_entry/<int:id>', methods=['GET', 'POST'])
@login_required
def comment_entry(id):
title = 'Показать стих'
comment_entry = Entry.query.get_or_404(id)
form = CommentForm()
if current_user and form.validate_on_submit():
comment_add = Comment(body=form.body.data,
pub_date=datetime.now(),
author_comment=current_user,
entry_comment=comment_entry)
db.session.add(comment_add)
db.session.commit()
flash('Вы добавили свой комментарий')
return redirect(url_for('comment_entry', id=comment_entry.id))
return render_template('entry/comment_entry.html',
title=title, entry=comment_entry, form=form)
{% extends 'layout/base.html' %}
{% block content %}
<div class="show_entry">
<img src="{{ entry.users.avatar(130) }}">
<p>Пользователь: <strong>{{ entry.users.nickname }}</strong></p>
<p><strong>Тема: {{entry.title}}</strong></p>
<pre><big>{{ entry.text }}</big></pre>
<p>{{ entry.pub_date }}</p>
<br>
</div>
<form method=post action=comment_entry align=center>
{{ form.hidden_tag() }}
<p><kbd>{{ form.body.label }}:</kbd></p>
<pre>{{ form.body(cols=52, rows=4) }}</pre>
<div class="bg-info">
{% for error in form.errors.body %}
<span>{{ error }}</span>
{% endfor %}
</div>
<button type=submit class="btn btn-success-outline">Добавить комментарий</button>
</form>
{% endblock %}
Answer the question
In order to leave comments, you need to log in
I don’t see sending id at all in your form .... Maybe this is done through javascript, then look at what the browser sends to you, and at what url. In the browser, turn on the mode for developers, all requests are visible there.
Well, there should be something like this in the template
<form ... action="{{url_for('.comment_entry',id=entry.id)}}" ... > ...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question