diff --git a/datasette/app.py b/datasette/app.py index af3dcc8b4d..90abc3732d 100644 --- a/datasette/app.py +++ b/datasette/app.py @@ -739,6 +739,7 @@ async def render_template( "zip": zip, "body_scripts": body_scripts, "format_bytes": format_bytes, + "show_messages": lambda: self._show_messages(request), "extra_css_urls": self._asset_urls("extra_css_urls", template, context), "extra_js_urls": self._asset_urls("extra_js_urls", template, context), "base_url": self.config("base_url"), diff --git a/datasette/views/base.py b/datasette/views/base.py index 208c3c9690..6346a3f576 100644 --- a/datasette/views/base.py +++ b/datasette/views/base.py @@ -92,7 +92,6 @@ async def render(self, templates, request, context=None): **{ "database_url": self.database_url, "database_color": self.database_color, - "show_messages": lambda: self.ds._show_messages(request), "select_templates": [ "{}{}".format( "*" if template_name == template.name else "", template_name diff --git a/tests/plugins/my_plugin.py b/tests/plugins/my_plugin.py index bf6340ce1b..8701c6db8c 100644 --- a/tests/plugins/my_plugin.py +++ b/tests/plugins/my_plugin.py @@ -192,10 +192,13 @@ def not_async(): def add_message(datasette, request): datasette.add_message(request, "Hello from messages") - print("Adding message") - print(request._messages) return Response.html("Added message") + async def render_message(datasette, request): + return Response.html( + await datasette.render_template("render_message.html", request=request) + ) + return [ (r"/one/$", one), (r"/two/(?P.*)$", two), @@ -204,6 +207,7 @@ def add_message(datasette, request): (r"/csrftoken-form/$", csrftoken_form), (r"/not-async/$", not_async), (r"/add-message/$", add_message), + (r"/render-message/$", render_message), ] diff --git a/tests/test_plugins.py b/tests/test_plugins.py index 9468fde926..9a2ee2a35a 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -610,6 +610,16 @@ def test_register_routes_add_message(app_client): assert [["Hello from messages", 1]] == decoded +def test_register_routes_render_message(restore_working_directory, tmpdir_factory): + templates = tmpdir_factory.mktemp("templates") + (templates / "render_message.html").write_text('{% extends "base.html" %}', "utf-8") + with make_app_client(template_dir=templates) as client: + response1 = client.get("/add-message/") + response2 = client.get("/render-message/", cookies=response1.cookies) + assert 200 == response2.status + assert "Hello from messages" in response2.text + + @pytest.mark.asyncio async def test_startup(app_client): await app_client.ds.invoke_startup()