Skip to content

Instantly share code, notes, and snippets.

@AbreuY
Forked from johnsardine/force_external_url_for.py
Created February 1, 2022 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AbreuY/44a938e438b001a7e3e1593534589e13 to your computer and use it in GitHub Desktop.
Save AbreuY/44a938e438b001a7e3e1593534589e13 to your computer and use it in GitHub Desktop.
Force Flask url_for to use domain name (absolute url)
# In manage.py add
# Make sure you import: from flask import url_for
@app.context_processor
def override_url_for():
return dict(url_for=external_url_for)
def external_url_for(endpoint, **values):
# Force absolute url in assets
if app.config.get('FORCE_EXTERNAL_URL'):
values['_external'] = True
return url_for(endpoint, **values)
# In config.py add
# Make sure you import: import os
FORCE_EXTERNAL_URL = \
os.environ.get("FORCE_EXTERNAL_URL", "False") == "True"
# In your .env add
FORCE_EXTERNAL_URL="True"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment