无法发布wtf-form-使用flask获得200响应而不是302,并且发布未出现在我的数据库中

时间:2019-10-16 06:55:53

标签: python jquery flask jinja2 flask-wtforms

我正在尝试从引导程序模式提交wtf-form,但是在提交时,该模式将按预期关闭,并且请求方法为“ POST”,但状态为200,并且表单未通过表单验证功能来发布表单输入数据库。

表单控制器:

@login_required
def modal():
    form = ModalForm()
    print("Pre-Validation")
    if form.validate_on_submit():
        print(form.errors)
        print("Validating")
        post = Post(title=form.title.data, content=form.content.data, author= current_user)
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('main.home'))
    return render_template('modal.html', title = 'modal', form=form, legend='Create Post')

表单html代码:

<div class="modal fade" id="teleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">New message</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form method="POST" id="form1" action="">
                {{ form.hidden_tag() }}
                <div class="form-group">
                    {{ form.title.label(class="form-control-label") }}
                    {% if form.title.errors %}
                        {{ form.title(class="form-control form-control-lg is-invalid") }}
                        <div class="invalid-feedback">
                            {% for error in form.title.errors %}
                                <span>{{ error }}</span>
                            {% endfor %}
                        </div>
                    {% else %}
                        {{ form.title(class="form-control form-control-lg") }}
                    {% endif %}
                </div>
                <div class="form-group">
                    {{ form.content.label(class="form-control-label") }}
                    {% if form.content.errors %}
                        {{ form.content(class="form-control form-control-lg is-invalid") }}
                        <div class="invalid-feedback">
                            {% for error in form.content.errors %}
                                <span>{{ error }}</span>
                            {% endfor %}
                        </div>
                    {% else %}
                        {{ form.content(class="form-control form-control-lg") }}
                    {% endif %}
                </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                {{ form.submit(class="btn btn-outline-info", id='btnSave') }}
            </div>
            </div>
        </div>
    </div>

Jquery部分用于模式加载和提交:

  <script>
        $('#teleModal').on('show.bs.modal', function (event) {
            var button = $(event.relatedTarget) 
            var recipient = button.data('whatever')
            var modal = $(this)
            modal.find('.modal-title').text('New message to ' + recipient)
            modal.find('.modal-body input').val(recipient)
        })
        $('#btnSave').click(function() {
            $('#teleModal').trigger('click');
            $('#form1').submit();
        })
    </script>

在页面上登陆后:Pre-Validation 127.0.0.1 - - [16/Oct/2019 02:40:33] "GET /modal HTTP/1.1" 200 -

提交帖子并关闭模式后:

127.0.0.1 - - [16/Oct/2019 02:40:31] "POST /modal HTTP/1.1" 200 -
Pre-Validation
127.0.0.1 - - [16/Oct/2019 02:40:33] "GET /modal HTTP/1.1" 200 -

0 个答案:

没有答案