【问题标题】:Passing information from one route to another: Flask ignoring redirect requests将信息从一条路线传递到另一条路线:Flask ignoring redirect requests
【发布时间】:2019-06-24 04:53:07
【问题描述】:

我无法让烧瓶将信息发送到要提交的不同路径。将来,这将用于需要在已注销用户可以查看的页面上登录的操作。

我正在使用 python 3.6 和烧瓶 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

【问题讨论】:

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


    【解决方案1】:

    您没有对视图返回任何响应。

    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')
    

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

    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)
    

    【讨论】:

    • 抱歉,我的问题中缺少信息。在示例中,我试图在重定向时在服务器端打印表单数据。问题是在重定向时没有任何东西打印在任何地方。
    • 您希望将结果输出到控制台?
    • 我想是的,是的。
    • 这并没有达到我的预期。我添加了所需输出的屏幕截图。
    • 我再次更新了答案。我现在在移动设备上,无法在格式化时做得更好更快。
    猜你喜欢
    • 2021-08-20
    • 1970-01-01
    • 2016-04-22
    • 2017-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-01
    • 2021-12-29
    相关资源
    最近更新 更多