Simple question, how can I build URL with parameters for external websites?
Flask helper url_for
is really handy to build internal links and I was wandering if it could also be used for external links, but it seems that this is not as simple as it sounds.
<code><a href="{{ url_for('endpoint', foo='bar') }}">internal</a>
<a href="{{ url_for('https://example.net/', foo='bar', _external=true) }}">external (doesn't work)</a>
</code>
<code><a href="{{ url_for('endpoint', foo='bar') }}">internal</a>
<a href="{{ url_for('https://example.net/', foo='bar', _external=true) }}">external (doesn't work)</a>
</code>
<a href="{{ url_for('endpoint', foo='bar') }}">internal</a>
<a href="{{ url_for('https://example.net/', foo='bar', _external=true) }}">external (doesn't work)</a>
<code>from flask import Flask, url_for
app = Flask(__name__)
app.config["SERVER_NAME"] = "example.com"
app.config["PREFERRED_URL_SCHEME"] = "https"
with app.app_context():
# Expected: "https://example.net/?foo=bar"
print(url_for("https://example.net/", foo="bar", _external=True))
</code>
<code>from flask import Flask, url_for
app = Flask(__name__)
app.config["SERVER_NAME"] = "example.com"
app.config["PREFERRED_URL_SCHEME"] = "https"
with app.app_context():
# Expected: "https://example.net/?foo=bar"
print(url_for("https://example.net/", foo="bar", _external=True))
</code>
from flask import Flask, url_for
app = Flask(__name__)
app.config["SERVER_NAME"] = "example.com"
app.config["PREFERRED_URL_SCHEME"] = "https"
with app.app_context():
# Expected: "https://example.net/?foo=bar"
print(url_for("https://example.net/", foo="bar", _external=True))
url_for
has _external
option, but it seems different from what I imagined.