Reputation: 180
This is a Python/Flask question.
UPDATE: I woke up this morning with a bad feeling, and rightly so. Flask is not creating these grotesque hrefs in the source code - the prepending is taking place during execution. I have been looking at the link addresses displayed as I moused over the links. Today I checked them in the generated source, and they are fine - exactly as I generated them.
So now my question is: Why is the runtime process building some sort of floating base URL as I move from one project to the next? How do I turn that off - or tell Flask to reset to the original root application directory?
= = = = =
I am trying to generate a web page that includes references to related web pages. The pages are organized in a tree-like structure with projects all at one level and multiple pages under each project. For each page shown, I want to show a sidebar listing all projects and all pages under each project with links for each page shown.
My views.py program is set up as follows:
@app.route('/')
@app.route('/abc')
@app.route('/abc/<projectName>')
@app.route('/abc/<projectName>/<pageName>')
@app.route('/<projectName>/<pageName>')
def abc_pages(projectName=None, pageName=None):
.
. [includes creating anchor tags]
.
My problem is that, when my URL contains both a projectName and a pageName, the anchor tags I create get the projectName prepended to them. As an example, I can get to a default page using a URL with only the projectName like this: "example.com:5000/abc/projectA". On that page the generated anchor tags are as expected with href that look like "example.com:5000/abc/projectB/page_b2".
But if I get to a page using a URL with both the projectName and the pageName like "example.com:5000/abc/projectA/page_a1", the anchor (generated just as before) looks like this: "example.com:5000/abc/projectA/projectB/page_b2".
After a long, frustrating search came up with no solution, I roll with it and and just trim off the offending projectName like this:
@app.route('/')
@app.route('/abc')
@app.route('/abc/<projectName>')
@app.route('/abc/<projectName>/<pageName>')
@app.route('/<projectName>/<pageName>')
@app.route('/abc/<noName>/<projectName>/<pageName>') <<--- new code here
def abc_pages(noName=None, projectName=None, pageName=None):
.
. [includes creating anchor tags]
.
No joy. That only gets me through one iteration of the problem. I can click on the malformed "example.com:5000/abc/projectA/projectB/page_b2", but the resulting generated anchor tags on page_b2 look like "example.com:5000/abc/projectA/projectB/ProjectC/page_c3".
So my question is why the generated anchor tags get this history (but only the projectName history) prepended to them. I'm generating the same code for the anchor tag each time, but the href keeps expanding.
Upvotes: 1
Views: 679
Reputation: 67489
The Flask friendly way to generate links is via the use of url_for, because that eliminates the dependency of you having to know how the routes are built.
Example:
link = url_for("abc_pages", projectName = "project_B", pageName = "page_B2")
If you generate your links this way you have nothing to worry about, they will always be correct. And if later you decide to make changes to your routes url_for
will also update on its own.
That said, the problem that you have with your hand-generated routes is likely that you are making them relative by not starting them with a /
. A link that does not begin with a /
is relative to the URL shows in the address bar of the browser, so for example, if your browser is at /project_A
and you have a link to project_B/page_B2
the browser will concatenate these two and go to /project_A/project_B/page_B2
. If instead, you write your link as /project_B/page_B2
then this is an absolute URL so the browser will toss away the old path and use this one as the new URL.
Upvotes: 1