Last active
October 17, 2021 14:56
-
-
Save dejurin/85190a76289c3921caf9f0e08f6ec472 to your computer and use it in GitHub Desktop.
Starlette Jinja2Templates add relative URL (url_path_for)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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