2

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?

Alexander
  • 73
  • 2
  • 2
  • 5

2 Answers2

4

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.

sarartur
  • 1,178
  • 1
  • 4
  • 13
2
<a href="{{url_for(app.login)}}" >Login</a>
weijiang1994
  • 311
  • 1
  • 4
  • 1
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Donald Duck Nov 30 '20 at 10:13