Skip to content

Instantly share code, notes, and snippets.

@dejurin
Last active October 17, 2021 14:56
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 dejurin/85190a76289c3921caf9f0e08f6ec472 to your computer and use it in GitHub Desktop.
Save dejurin/85190a76289c3921caf9f0e08f6ec472 to your computer and use it in GitHub Desktop.
Starlette Jinja2Templates add relative URL (url_path_for)
#Add to class Jinja2Templates:
#https://github.com/encode/starlette/blob/6c556f6c5e4aa70173a84f6e6854390241231021/starlette/templating.py
@jinja2.contextfunction
def url_path_for(context: dict, name: str, **path_params: typing.Any) -> str:
request = context["request"]
router = request.scope["router"]
return router.url_path_for(name, **path_params)
env.globals["url_path_for"] = url_path_for
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment