Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3001,7 +3001,15 @@ async def handle_404(self, request, send, exception=None):
for regex, wildcard_template in page_routes:
match = regex.match(route_path)
if match is not None:
context.update(match.groupdict())
# Tilde-decode captured path variables so templates see
# the real value - e.g. "hello world" not "hello~20world"
# https://github.com/simonw/datasette/issues/1732
context.update(
{
key: tilde_decode(value)
for key, value in match.groupdict().items()
}
)
template = wildcard_template
break

Expand Down
18 changes: 18 additions & 0 deletions tests/test_custom_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,24 @@ def test_custom_route_pattern(custom_pages_client, path, expected):
assert response.text.strip() == expected


@pytest.mark.parametrize(
"path,expected",
[
# Tilde-encoded path variables should reach the template decoded
# https://github.com/simonw/datasette/issues/1732
("/topic_hello~20world", "Topic page for hello world"),
("/topic_caf~C3~A9", "Topic page for café"),
("/topic_a~2Fb/x~20y", "Slug: x y, Topic: a/b"),
],
)
def test_custom_route_pattern_decodes_path_variables(
custom_pages_client, path, expected
):
response = custom_pages_client.get(path)
assert response.status == 200
assert response.text.strip() == expected


def test_custom_route_pattern_404(custom_pages_client):
response = custom_pages_client.get("/route_OhNo")
assert response.status == 404
Expand Down
Loading