Passing information from one route to another: Flask ignoring redirect requests

Question:

I am having trouble getting flask to send information to a different route to be submitted. In the future this will be used for actions that require login on pages that logged out users can view.

I am using python 3.6 and flask 1.0.2. I have tried redirecting using validate_on_submit(), messing with various other parts of the code, and I have tried linking to the second route in 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)

In the example I am trying to print the form data on the server side on redirect. Assuming that is even possible.

<!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>

Both methods refresh the page without doing anything else. The problem is that on redirect nothing prints anywhere.

in this picture you can see the print(url_for(‘submit’, content=content)) output I want to do a similar thing with print(content) but the code never makes it there.
photo of output

Asked By: person

||

Answers:

You are not returning any response to the view.

return redirect(url_for())

And you must pass route decorator’s function name to the url_for() to generate url, not the template name.

For example:

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


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

To Print content to the flask development console.

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

In your case, you can pass data like below:

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)
Answered By: Debendra
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.