-
-
Notifications
You must be signed in to change notification settings - Fork 933
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow "name" argument on templates url_for() #2127
Conversation
Hmm... Not sure if we should do this... The |
How do you mean? The |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. I tested manually but maybe worth adding a simple test case.
At this point, why don’t we wait a bit and drop support for 3.7 first? Then we can make it a runtime enforced positional argument. |
But the context parameter shouldn't be... 🤔 |
Not sure what you mean. But also re-reading I realized we already merged a sibling PR, so no point in holding off. |
This was the test I made to make sure my change worked. I didn't add it to the PR because it seemed a bit trivial for something that will be redundant soon. I will add it if you want. def test_template_name_parameter(tmpdir, test_client_factory):
path = os.path.join(tmpdir, "index.html")
with open(path, "w") as file:
link = "<a href='{{ url_for('homepage', name=name) }}'>{{ name }}</a>"
file.write(f"<html>Hello, { link }</html>")
async def homepage(request):
return templates.TemplateResponse(
"index.html", {"request": request, "name": request.path_params["name"]}
)
app = Starlette(
debug=True,
routes=[Route("/{name}", endpoint=homepage)],
)
templates = Jinja2Templates(directory=str(tmpdir))
client = test_client_factory(app)
response = client.get("/world")
exptected_html = "<html>Hello, <a href='http://testserver/world'>world</a></html>"
assert response.text == exptected_html
assert response.template.name == "index.html"
assert set(response.context.keys()) == {"request", "name"} |
My point is... There's a parameter |
Ah, my bad. I was actually saying nonsense. |
Summary
#2050 fixed the url_for functions not allowing "name" as a parameter, but it missed the templating url_for function
Checklist