Python Forum

Full Version: Python redirect users to another url after form post
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am Not using django or flask. How to redirect to outside URL. Such as google com or /index.html This code does not work.
return { Location : "/thank-you.html" }

I want the equivalent of

return HttpResponseRedirect('/thanks/') #Redirect after POST

I can't use javascript for this. The script needs to run completely. As of right now users are sent to the python.script.url and thats all. I need them to go to a different one.
Is there a restriction to use Flask/Django?

You can use Flask in a very simple way to do this:

from flask import Flask, redirect


app = Flask(__name__)


@app.route("/index.html")
@app.route("/")
def go_to_external_url():
    return redirect('http://google.com')


if __name__ == "__main__":
    app.run(host='0.0.0.0', port=4000)
I am not using either one. However, can you cut out the redirect and flask requirements to do the redirects. That sounds like the best idea to me.
How are you serving the HTTP Form?
No information will go to the redirect. I simply want the user to go to the Thank you page or a URL after the script has ran through or when it has completed.

I tried Javascript and ajax. and that was a failure. I do not have the knowledge to do this. The form is a sign up form and it can take several seconds for everything to load and the python script to be done. If I could do something that salesforce.com uses. The cloud when you sign up. That would be the preferred method. THis is more of a temporary thing till I can pay someone.
Look if this is what you want...

<!DOCTYPE html>
    <html>
        <head>
          <meta charset="utf-8">
          <title>Redirect Test</title>
        </head>

        <body>

        <form action="/" method="POST">
          First name:<br>
          <input type="text" name="firstname"><br>
          Last name:<br>
          <input type="text" name="lastname">
          <br>
          <input type="submit" value="Submit" onclick="redirect()">
        </form>

        <script>
            var redirect = function() {
                 setTimeout(function(){
                     window.location.href = 'http://www.google.com/';
                     }, 1);
                 };
        </script>

        </body>
</html>