-
-
Notifications
You must be signed in to change notification settings - Fork 339
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
bug: Plugin "Flash Messages" not working with "Redirect" Response #3325
Comments
@euri10? 👀 |
If it helps, this is how we are doing it manually in Litestarfrom litestar import Litestar, Request, get, post
from litestar.contrib.jinja import JinjaTemplateEngine
from litestar.middleware.session.server_side import ServerSideSessionConfig
from litestar.response import Template, Redirect
from litestar.template.config import TemplateConfig
template_config = TemplateConfig(engine=JinjaTemplateEngine, directory="templates")
def flash(request: Request, message: str, category: str = "primary") -> None:
if "_messages" not in request.session:
request.session["_messages"] = []
request.session["_messages"].append({"message": message, "category": category})
def get_flashed_messages(request: Request) -> list[str]:
return request.session.pop("_messages") if "_messages" in request.session else []
template_config.engine_instance.engine.globals["get_flashed_messages"] = get_flashed_messages
@get('/')
async def index() -> Redirect:
return Redirect('/login')
@get('/login')
async def login(request: Request) -> Template:
flash(request, "Flash Test!", category="info")
return Template(template_str='{% for f in get_flashed_messages(request) %} <p>{{ f.message }}</p>{% endfor %}'
'<form action="/check" method="post"><button type="submit">login</button></form>')
@post('/check')
async def check(request: Request) -> Redirect:
flash(request, "User not Found!", category="warning")
return Redirect('/login')
app = Litestar(route_handlers=[index, login, check], template_config=template_config,
middleware=[ServerSideSessionConfig().middleware])
if __name__ == '__main__':
import uvicorn
uvicorn.run('1:app')
|
We hoped 'flash Messages' would be integrated into the core functionality rather than as a plugin, which can often get abandoned. It's a feature present in Flask and Django, so it's likely quite important. Nonetheless, appreciate the work done so far. Litestar is at its best and continually improving. That's wonderful. Thank you! |
Configuring Litestar via plugins is an explicit architectural decision - we also use them extensively for internal things. This plugin is in the main code base and is maintained alongside the rest of the library code so there should be no need to be concerned about commitment to its maintenance any more than any other part of the library. |
ok I have the same issue now that I'm trying to switch from my implenetation (which is exactly the same as @saltcable ) to the official plugin:) I'm not sure why I went passing the messages in the request state rather than in the session in the plugin though. This said, this could be either fixed using the same method for passing flash messages around ie in a session (but that means the plugin requires to have the session middleware installed, we could check for that in the app_init and / or install a default config for it, I'd prefer just raise a misconfiguration error rather than providing a default config) |
This issue has been closed in #3420. The change will be included in the upcoming patch release. |
A fix for this issue has been released in v2.9.0 |
Litestar (2.8.0)
Problem: Flash Message from "POST /check" is missing at final page (screenshot above)
Flask (3.0.2)
Note
While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.
Check out all issues funded or available for funding on our Polar.sh dashboard
The text was updated successfully, but these errors were encountered: