Skip to content

Instantly share code, notes, and snippets.

@dejurin
Last active Oct 17, 2021
Embed
What would you like to do?
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