如何制作 django 消息 StackOverflow 风格?
我想使用 Django 的消息模块,但是,我希望我的消息一直持续到用户单击消息旁边的 X,而不是在用户重新加载页面时消息就消失。
我被两个问题难住了: 如何使消息的上下文处理器在访问消息后不删除消息? 一旦用户单击“删除”按钮(调用 ajax 调用),我如何从数据库中显式删除消息?
谢谢!
I'd like to use Django's Messages module, however, I would like my messages to persist until the user clicks an X next to the message as opposed to having messages disappear as soon as the user reloads the page.
I am stumped with two issues:
How do I make the messages' context processor not delete the messages once they are accessed?
How do I later on delete the message from the DB explicitly once the user clicks on the "remove" button (which invokes an ajax call)?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在你的情况下,
django.contrib.messages
不会给你带来任何好处。这是一个受 RoR flash 系统启发的消息系统,消息不应该保留在周围。您应该创建自己的消息系统(也许是 django-persistent-messages?),它将在数据库中保存注册用户的消息。
不要忘记将其提供给其他人如果你这样做=)
In your case
django.contrib.messages
won't bring you anywhere good. It's a message system inspired by RoR flash system, where messages aren't supposed to stay aroundYou should create your own messaging system (django-persistent-messages maybe?) that would save messages for registered users in database.
Don't forget to make it available to others if you do so =)
从 1.2 开始,Django 有了一个新的消息框架——< code>django.contrib.messages——现在完全脱离了
auth
模块并提供了更多的功能。例如,它提供了处理 过期的基本方法消息数。您还可以查看 django-cnotes 应用程序,它提供了一个简单的基于 cookie 的用户通知系统。将常量
CNOTES_AUTO_CLEAR
设置为False
可防止自动清除注释。还有 django-notices,这是内置的另一个替代品在消息通知系统中。它没有什么神奇之处,但提供了一个优雅而简单的 API。
Since 1.2, Django has a new messages framework--
django.contrib.messages
--that is now completely detached from theauth
module and offers much more functionality. For instance, it provides a basic way of handling the expiration of messages.You can also take a look at the django-cnotes application that provides a simple cookie based user notification system. Setting constant
CNOTES_AUTO_CLEAR
toFalse
prevents clearing the notes automatically.And there is django-notices, yet another replacement for the built-in message notification system. It does no magic but provides an elegant and simple API.
自 2010 年底以来,库 django-persistent-messages 正是为了实现这一目标。它有相当完善的文档记录,并且可以很好地创建 Stack Overflow 风格的消息传递系统。
它还与内置的 Django 消息系统集成,因此代码更改相对较小,对于不需要持久化的消息,您仍然可以使用原始系统。
Since late 2010, there is the library, django-persistent-messages, for exactly this goal. It's reasonably well documented and works well to create a Stack Overflow-style messaging system.
It also integrates with the built-in Django messaging system, so the code changes are relatively minor, and you can still use the original system for messages that don't need to be persistent.
Django 消息可能看起来是一个很好的起点,但需要一些扭曲才能到达您想要去的地方,而且我不相信 Django 的未来版本不会破坏您的技巧。
从长远来看,实现您自己的 UserMessage 模型可能会为您提供更好的服务。这使您可以完全、明确地控制消息生命周期。它也可能成为一个很好的可重用应用程序。
Django messages might seem like a good starting point, but require contortions to get to where you want to go, and I wouldn't trust that future release of Django wouldn't break your hacks.
Implementing your own UserMessage model will probably serve you better in the long run. That gives you complete, unambiguous control over the message lifecycle. It might make a nice reusable app, too.