How to make a redirect button in Flask template

Viewed 11604

I am trying to make a web-app using flask, and I am having a problem redirecting to another page/route that I have in my app.py file. I also set up a HTML template for the other (login) page.

Here is my code in the main app.py:

@app.route('/', methods=['GET', 'POST']) 

    if request.method == 'GET':
        pass

    if request.method == 'POST':
        pass

    return render_template('index.html', passable=passable)

@app.route('/login', methods=['GET', 'POST'])
def login():

    if request.method == 'POST':
        name = request.form.get('username')
        post = request.form.get('password')
        # still need to complete

    return render_template('login.html')

I have imported all the relevant modules (I think).

In my Index.html file, I have a button, which I would like it to redirect to my login.html page. Currently I am doing something like this:

<button type="submit" onclick='{{Flask.redirect(url_for(login))}}' value="editor">

Whenever I launch the page (locally) i get this error. jinja2.exceptions.UndefinedError jinja2.exceptions.UndefinedError: 'Flask' is undefined

How do I make my button redirect to the "login" flask route?

2 Answers

If you want your form to submit to a different route you can simply do <form action="{{ url_for('app.login') }}">.

If you just want to put a link to the page use the <a> tag.

If you want to process the request and then redirect, just use the redirect function provided by flask.

<a href="{{url_for(app.login)}}" >Login</a>
Related