是否有相当于Rails的“flash” GAE(python)中的消息? [英] Is there an equivalent to Rails’ "flash" messages in GAE (python)?

查看:122
本文介绍了是否有相当于Rails的“flash” GAE(python)中的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ruby on Rails有一种方法可以设置消息,如 flash [:notice] flash [:error ,用户将看到下一个机会。这对于通知用户登录失败等情况特别有用,当他们被重定向时(例如回到登录页面)。



有没有在Google App Engine(Python API)上实现此规范或常用方法? (假设Django未被使用)。

解决方案

我喜欢llazzaro关于模板的建议。







li>

如果您正在处理会话,请将该消息粘贴到会话中。

  • 如果您没有会话支持,您将拥有以创建使用cookie。



    Cookie 警告


    • 在GAE中设置cookie真的很难,因为你基本上必须自己设置标题。 (如果有GAE内置的API来设置cookie,请更正此问题;这是一个社区wiki)。谨防编码和其他限制(分号表示您的cookie值结束)。找到一个写得很好的函数来写入cookie并使用它。

    • 了解浏览器cookie长度限制

    • 如果您要发送预设消息,请考虑在cookie中设置消息的唯一标识符的实际消息。您不会遇到长度或编码方面的问题!

    • 如果您的消息是变量,则可能的解决方法就像上面的项目符号点一样,但是不是预设的消息,而是在数据存储对象设置消息,将其标识符写入cookie中,并在显示消息时,在数据存储中查找并擦除cookie。 / ol>

      无论如何,当您显示Flash消息时,请立即清除会话或Cookie中的消息。


      Ruby on Rails has a way that you can set a message, like flash[:notice] and flash[:error, that the user will see at the next opportunity. It’s especially useful for things like notifying the user of failure to log in, etc., when they are redirected (e.g. back to a sign-in page).

      Is there a canonical or common way to achieve this on Google App Engine (Python API)? (Assume Django is not being used.)

      解决方案

      I like llazzaro's advice regarding templates.

      The other half to the story is being able to maintain the flash message between requests.

      1. If you are dealing with sessions, stick the message in the session.

      2. If you don't have session support, you'll have to create use a cookie.

        Cookie caveats:

        • It’s surprisingly hard to set cookies in GAE because you basically have to set the header yourself. (Correct this if there is an API built in to GAE to set cookies; it's a community wiki). Beware of encoding and other limitations (semicolons indicate end of your cookie value). Find a well-written function to write cookies and use it.
        • Be aware of browser cookie length limits
        • If you are sending a preset message, consider just setting a unique identifier for the message in the cookie instead of the actual message. You won't have issues with length or encoding!
        • If your message is variable, one possible workaround is just like the above bullet point, but instead of preset messages, push a datastore object when setting a message, write its identifier to a cookie, and when displaying the message, look it up in the datastore and then wipe the cookie.

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

      这篇关于是否有相当于Rails的“flash” GAE(python)中的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆