4

How do I get the user input submitted on the form to be displayed on a fixed URL?

@app.route('/', methods=['GET','POST'])
def main():
    if request.method == 'POST':
        msg = request.form['message']
        return redirect(url_for('display_msg', msg=msg))
    else:
        return form


@app.route('/msg')
def display_msg(msg):
    return msg

When submitting the form, I get the following error:

TypeError: display_msg() missing 1 required positional argument: 'msg'

This code actually works fine if I initialize msg as a global variable and then do global msg at the top of main, but it will not allow me to pass msg as a parameter to display_msg.

Another way this will work is if I change @app.route('/msg') to @app.route('/<string:msg>'), but this will change the URL to whatever the user submitted on the form which is not what I want. I want the URL to be fixed.

3 Answers 3

7

Since the data from msg is already stored in the request object, rather than passing it as a parameter to display_msg, it can be accessed as follows:

@app.route('/msg')
def display_msg():
    return request.args.get('msg')

The TypeError occurred because the keyword arguments for url_for are passed to the request object and not as parameters to display_msg.

Sign up to request clarification or add additional context in comments.

Comments

2

You need to specify the msg parameter:

@app.route('/msg/<msg>')
def display_msg(msg):
    return msg

1 Comment

I want the URL for display_msg to be the same regardless of what the user inputs on the form. Please read the last few lines of my post.
1

You can use a global variable to store the message and return to a fixed url.

Edit: I updated display_msg() to use a default argument.

global_msg = ""
@app.route('/', methods=['GET','POST'])
def main():
    global global_msg
    if request.method == 'POST':
        global_msg = request.form['message']
        return redirect(url_for('display_msg'))
    else:
        return form


@app.route('/msg')
def display_msg(msg = global_msg):
    return msg

Hope this helped you.

NOTE:Please don't use this answer! what happens when multiple clients connect to your server and access the same route at once! they will actually share data between then!

4 Comments

Yes, I know that already. I'm asking why it doesn't work when I pass it locally as a parameter.
Can you explain why you are trying to get away from global member method? I modified the answer to accept direct calls to display_msg() by using default arguments.
Well, because it's generally good practice to avoid using global variables when possible. See my answer below.
Please don't use this answer! what happens when multiple clients connect to your server and access the same route at once! they will actually share data between then!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.