Issue
I have this code for redirecting the user to different subpages in Python flask:
@app.route('/')
def main():
return render_template('interface.html')
@app.route('/#ordergrade/')
def ordergrade():
return sort.orderbygrade(True, False).to_html()
interface.html
has this code:
<!DOCTYPE html>
<html>
<head>
<title>xxxxxxxxxxxxxx</title>
</head>
<body>
<h1>Options</h1>
<p>Click on the buttons below to be redirected to a web page.</p>
<button onclick="window.location.hash='#ordergrade/'">1</button>
<button onclick="window.location.hash='#orderuniversity/'">2</button>
<button onclick="window.location.hash='#showopts/'">3</button>
<button onclick="window.location.hash='#delete/'">4</button>
<button onclick="window.location.hash='#infnum/'">5</button>
<button onclick="window.location.hash='#supnum/'">6</button>
</body>
</html>
When clicking the first button, I get correctly redirected to the /#ordergrade
page, but only interface.html
is rendered. Why does that happen?
Everything seems to go correctly, except that nothing happens.
Solution
The fragment
of a URL (the part after the #
) is not sent to the server, so when you visit the path /#ordergrade/
the server only sees /
.
If you want a #
in the URL, you'll need to encode it using URL encoding, E.g.,
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index_view():
# Here, `%23` represents "#"
return '<a href="/%23test/">test</a>'
@app.route("/#test/")
def test_view():
return "worked!"
It's also possible to handle on the client, have a look at the browser/javascript hashchange
event, or consider using a frontend javascript framework that includes a routing solution.
Answered By - DanielB
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.