| 1 |
from litestar import Litestar, Request, get |
| 2 |
from litestar.contrib.jinja import JinjaTemplateEngine |
| 3 |
from litestar.middleware.session.server_side import ServerSideSessionConfig |
| 4 |
from litestar.plugins.flash import FlashConfig, FlashPlugin, flash |
| 5 |
from litestar.response import Template |
| 6 |
from litestar.template.config import TemplateConfig |
| 7 |
|
| 8 |
template_config = TemplateConfig(engine=JinjaTemplateEngine, directory="templates") |
| 9 |
flash_plugin = FlashPlugin(config=FlashConfig(template_config=template_config)) |
| 10 |
|
| 11 |
|
| 12 |
@get() |
| 13 |
async def index(request: Request) -> Template: |
| 14 |
"""Example of adding and displaying a flash message.""" |
| 15 |
flash(request, "Oh no! I've been flashed!", category="error") |
| 16 |
|
| 17 |
return Template( |
| 18 |
template_str=""" |
| 19 |
<h1>Flash Message Example</h1> |
| 20 |
{% for message in get_flashes() %} |
| 21 |
<p>{{ message.message }} (Category:{{ message.category }})</p> |
| 22 |
{% endfor %} |
| 23 |
""" |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
app = Litestar( |
| 28 |
plugins=[flash_plugin], |
| 29 |
route_handlers=[index], |
| 30 |
template_config=template_config, |
| 31 |
middleware=[ServerSideSessionConfig().middleware], |
| 32 |
) |