File: usage.py

package info (click to toggle)
litestar 2.19.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 12,500 kB
  • sloc: python: 70,169; makefile: 254; javascript: 105; sh: 60
file content (32 lines) | stat: -rw-r--r-- 1,067 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from litestar import Litestar, Request, get
from litestar.contrib.jinja import JinjaTemplateEngine
from litestar.middleware.session.server_side import ServerSideSessionConfig
from litestar.plugins.flash import FlashConfig, FlashPlugin, flash
from litestar.response import Template
from litestar.template.config import TemplateConfig

template_config = TemplateConfig(engine=JinjaTemplateEngine, directory="templates")
flash_plugin = FlashPlugin(config=FlashConfig(template_config=template_config))


@get()
async def index(request: Request) -> Template:
    """Example of adding and displaying a flash message."""
    flash(request, "Oh no! I've been flashed!", category="error")

    return Template(
        template_str="""
    <h1>Flash Message Example</h1>
    {% for message in get_flashes() %}
    <p>{{ message.message }} (Category:{{ message.category }})</p>
    {% endfor %}
    """
    )


app = Litestar(
    plugins=[flash_plugin],
    route_handlers=[index],
    template_config=template_config,
    middleware=[ServerSideSessionConfig().middleware],
)