将信息从一条路由传递到另一条路由:烧瓶忽略重定向请求

时间:2019-01-30 19:54:36

标签: python redirect flask flask-wtforms url-for

我无法让烧瓶将信息发送到另一条要提交的路线。将来,这将用于需要登录的页面上可以执行的操作,而注销的用户可以查看这些页面。

我正在使用python 3.6和flask 1.0.2。我尝试使用validate_on_submit()进行重定向,弄乱了代码的其他各个部分,并且尝试链接到html中的第二条路由。

from flask import Flask, render_template, url_for, redirect
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField

app = Flask(__name__)
app.config['SECRET_KEY'] = 'b317a06ad972917a84be4c6c14c64882'


class PostForm(FlaskForm):
    content = StringField('Content')
    submit = SubmitField('form submit')


@app.route("/", methods=['GET', 'POST'])
@app.route("/home", methods=['GET', 'POST'])
def home():
    form = PostForm()
    if form.validate_on_submit():
        content = form.content.data
        redirect(url_for('submit', content=content))
        print(url_for('submit', content=content))
    return render_template('example.html', form=form)


@app.route("/submit/<string:content>", methods=['GET', 'POST'])
def submit(content):
    print('content')
    print(content)
    return redirect(url_for('example'))


if __name__ == "__main__":
    app.run(debug=True)

在示例中,我尝试在重定向时在服务器端打印表单数据。假设甚至有可能。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div class="content-section">
    <form method="POST" action="">
        {{ form.hidden_tag() }}
        <fieldset class="form-group">
            <legend class="border-bottom mb-4">{{ legend }}</legend>
            <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>
        </fieldset>
        <div class="form-group">
            <form action="{{ url_for('submit', content=content) }}" method="POST">
                <input class="btn btn-danger" type="submit" value="html submit">
            {{ form.submit(class="btn btn-outline-info") }}
        </div>
    </form>
</div>
</body>
</html>

这两种方法都可以刷新页面,而无需执行其他任何操作。问题是在重定向上,任何地方都无法打印。

在这张图片中,您可以看到 print(url_for('submit',content = content))输出,我想对 print(content)做类似的事情但是代码从来没有做到这一点。 photo of output

1 个答案:

答案 0 :(得分:0)

您不会对视图返回任何响应。

return redirect(url_for())

并且必须将路由装饰器的函数名称传递到url_for()才能生成url,而不是模板名称。

例如:

@app.route('/')
def index():
    return render_template('index.html')


# redirect `/somewhere/` to `/`
@app.route('/somewhere/')
     return redirect(url_for('index')

将内容打印到flask开发控制台。

import sys
print('This will be printed to the console', file=sys.stdout)

根据您的情况,您可以像下面那样传递数据:

import sys

@app.route("/", methods=['GET', 'POST']) 
@app.route("/home", methods=['GET', 'POST'])
def home():
    form = PostForm()
    if form.validate_on_submit():
        content = form.content.data
        print(content, file=sys.stdout)
        return redirect(url_for('submit', content=content))
    return render_template('example.html', form=form)
相关问题