S
S
sergey199408082017-04-17 21:11:51
Flask
sergey19940808, 2017-04-17 21:11:51

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)

I get a 404 error when posting a comment.

Here is the template for this view:
{% 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 %}

Why am I getting such an error?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Cheremisin, 2017-04-17
@sergey19940808

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 question

Ask a Question

731 491 924 answers to any question