0

I'm trying to develop a website using flask and python. I've come to a point where I'm trying to create a login for login experience and can't quite get it. I'll jump to an example:

elif request.method == 'POST':
<<do stuff>>
return render_template(
    'something.html',
    somedata
)

But the url doesn't change. That is perfectly fine in some cases, but I want it to change from say /login to /main for 2 reasons. a) aesthetic (well, I could work this one out with a bit of creativity) b) I don't think there is a way to handle 2 different POST "events" (or is there?).

Also I'm quite concerned with the POST part. What if I need 2 different buttons on the same page, is it possible to work around that? Or am I missing something?

Forge
  • 6,538
  • 6
  • 44
  • 64
4c74356b41
  • 69,186
  • 6
  • 100
  • 141
  • I think you need to redirect to another route, then handle that. Does [this](http://stackoverflow.com/questions/14343812/redirecting-to-url-in-flask) help? – Matt Hall Mar 17 '16 at 17:42
  • 1
    You need something called [redirect](https://en.wikipedia.org/wiki/URL_redirection#HTTP_status_codes_3xx), which is in Flask may be implemented using the function [`redirect`](http://flask.pocoo.org/docs/0.10/quickstart/#redirects-and-errors). – bereal Mar 17 '16 at 17:43
  • wow, cant believe I didn't find that one :( so I have to pass variables with "response"? – 4c74356b41 Mar 17 '16 at 17:46

1 Answers1

1

You're going to want to redirect to the other url. Without knowing what library you're using to log users in it's harder to tell you specifically how to log them in but this is the general idea.

from flask import Flask, redirect, request, render_template
app = Flask("app_name")

@app.route('/', methods=["GET", "POST"])
def login_route():
    if request.method == "GET":
        return render_template("login_page.html")
    elif request.method == "POST":
        # Login user
        return redirect("main_page", code=303)

@app.route("/")
def main_page():
    return "I'm the main page."

app.run(host="0.0.0.0", port=5001)
davidism
  • 121,510
  • 29
  • 395
  • 339
AlexLordThorsen
  • 8,057
  • 5
  • 48
  • 103