Skip to content

Get a route handler name in a template? #2989

Answered by Alc-Alc
4l1fe asked this question in Q&A
Discussion options

You must be logged in to vote

Heya, what you propose url_for(request.route_handler.name) already works inside a template. request is passed by default into the template context.

Here is an example that uses HTMX and Jinja as two separate routes. The assert passes, meaning it does have access and is able to get the name.

from litestar import get
from litestar.contrib.htmx.request import HTMXRequest
from litestar.contrib.htmx.response import HTMXTemplate
from litestar.contrib.jinja import JinjaTemplateEngine
from litestar.response import Template
from litestar.template.config import TemplateConfig
from litestar.testing import create_test_client


@get("hello_htmx", name="hello_htmx_route")
async def hello_htmx() -> HTMX…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
2 replies
@4l1fe
Comment options

@provinzkraut
Comment options

Answer selected by 4l1fe
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants