Home > Back-end >  How to redirect to another page with a button?
How to redirect to another page with a button?

Time:01-08

trying to avoid JS as much as possible, so I was wondering if there was a way to get redirects by using a button? I've been searching and trying, but can't find a solution that works.

Structure:

main.py
static/
   templates/
      index.html
      404.html
   styles/
      index.css

Python code:

from flask import Flask, redirect, url_for, render_template

app = Flask(__name__)

@app.route("/")
def home():
    return render_template("index.html")

@app.errorhandler(404)
def page_not_found(e):
    return render_template("404.html")

app.run(debug=True)

HTML of 404.html, which has a button I want to redirect back to home

<div class = "btn-bot">
<button  type="button" role="button" href="{{ url_for('home') }}">Return</button>
</div>

Clicking the buttons and all that stuff works just fine, but it never does any action or redirecting. Console outputs nothing.

CodePudding user response:

It's normal, you should use tag a.

<div class = "btn-bot">
<a href="{{ url_for('home') }}">
    <button  type="button" role="button">Return</button>
</a>
</div>

I think it's easy to understand, isn't it?

CodePudding user response:

Answer was to change the button line to

<a href="/home"><button  type="button" role="button">Return</button></a>

and in the Python file, just added /home as another route to the index

  •  Tags:  
  • Related