1

I'm having some trouble using url_for in Flask. I have a template with a button:

<a class="btn btn-primary btn-block" href="{{ url_for('login', provider='google') }}" role="button">
    <i class="fa fa-google fa-lg"></i>  Login with <strong>Google</strong>
</a>

Now, when I press the button my URL changes from http://localhost:5000/login to http://localhost:5000/login?provider=google.

Then my python code:

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

I do not know how to modify my python code so that it can handle this provider.

The documentation isn't very clear about this, at least I haven't been able to understand it.

Thanks!

EDIT:

I've tried what seemed obvious with no success:

@app.route("/login?provider=<provider>")
def login(provider):
    if provider == 'google':
        return "Hello, Google!"
    return render_template('login.html')

But now it wont find http://localhost:5000/login, wich is kind of obvious also.

EDIT 2:

I managed to make it work, but I don't know if it's a good practice:

@app.route("/login")
@app.route("/login?provider=<provider>")
def login(provider = None):
    if provider == 'google':
        return "Hello, Google!"
    return render_template('login.html')

0 Answers0