Is there an equivalent to Rails "flash" messages in GAE (python)?

Ruby on Rails has a way that you can set a message, for example flash[:notice] and flash[:error , which the user will see at the next opportunity. This is especially useful for things like notifying the user about the impossibility of logging in, etc., When they are redirected (for example, back to the login page).

Is there a canonical or general way to achieve this in the Google App Engine (Python API)? (Assume Django is not used.)

+4
google-app-engine
Feb 07 2018-11-21T00:
source share
5 answers

I like llazzaro's advice on templates.

The other half of the story is to support flash between requests.

  • If you are dealing with sessions, insert a message into the session.

  • If you do not have session support, you will need to create a cookie.

    Cautions for cookies:

    • It's unexpectedly difficult to set cookies in GAE because you basically need to set the header yourself. (Fix this if there is an API built into GAE for setting cookies, this is a wiki community). Beware of coding and other restrictions (semicolons indicate the end of your cookie value). Find a well-written function for writing cookies and its use.
    • Know browser cookie length limits
    • If you are sending a pre-configured message, consider setting a unique message identifier in the cookie instead of the actual message. You will not have problems with the length or encoding!
    • If your message is variable, one possible workaround is similar to the above marker point, but instead of the specified messages, click on the data store object when setting up the message, write its identifier to the cookie and looking at the message, see it in the data store, and then wipe the cookie.

Regardless, when you display flash messages, immediately clear the message from the session or cookie.

+1
Feb 11 2018-11-11T00:
source share

The webapp framework , the simple web application framework shipped with GAE, does not provide anything like this.

One cool framework designed specifically for the Google App Engine that offers Flash messaging is Tipfy .
Take a look at the tipfy.ext.session module:

set_flash (data, key = no, backend = None, ** kwargs)

Sets a flash message. On first reading, flash messages are deleted.

+2
Feb 07 2018-11-22T00:
source share

What do you think about expanding the template and setting the "flash" parameter to the template?

for example, a basic template:

 <html... bla blah ... <body ... bla blah {% if flash %} {{flash}} {% endif %} <!-- more html here --> {% block content %} your dynamic block here... {% endblock %} 

now in every template

 {% extends "base_template.html" %} {% block content %} {% if object %} success to edit : {{object.title }} {% endif %} {% endblock %} 

your handler should pass the flash-template to the template, it will be used in the base template.

+1
Feb 09 2018-11-11T00:
source share

Well, webapp2 has:

 def add_flash(self, value, level=None, key='_flash'): 

and

 def get_flashes(self, key='_flash'): 

Saves your messages and deletes them when reading. To show them to the user, you just need to set the variable in the render_template base request render_template . Something like that:

 def render_template(self, template, context=None): context = context or {} extra_context = { 'uri_for': self.uri_for, 'flashes': self.session.get_flashes(), 'user': self.current_user, } # Only override extra context stuff if it not set by the template: for key, value in extra_context.items(): if key not in context: context[key] = value rendered = self.jinja2.render_template(template, **context) self.response.write(rendered) 

And in your template, use the 'flash' variable to display your messages as you like.

Docs here: http://code.google.com/p/webapp-improved/source/browse/webapp2_extras/sessions.py?r=9c1ec933be7c3d8c09c9bf801ebffe2deeb922e0#127

Example: here https://simpleauth.appspot.com/

and example source: http://code.google.com/p/gae-simpleauth/source/browse/example/handlers.py

By the way, great work with simpleauth Alex!

+1
Dec 11
source share

Yes, look at this get_flashes(key='_flash')[source] function in this object: class webapp2_extras.sessions.SessionDict(container, data=None, new=False)[source]

Returns the flash message. On first reading, flash messages are deleted. Parameters: key - the name of the flash key stored in the session. The default is "_flash". Returns:
Data stored in flash memory or an empty list.

+1
Nov 05 '15 at 17:06
source share



All Articles