Django 从自定义中间件添加 contrib.messages

发布于 2024-11-07 19:28:14 字数 534 浏览 1 评论 0原文

我已经实现了一个自定义中间件来检查用户个人资料中的某些字段。注册时不需要这些字段(为了让用户快速轻松地注册),但是,我更希望他们填写这些字段。

我的中间件检查它们是否已设置。如果没有,它会通过消息通知用户。我遇到了两个问题。

  1. 每当我提交发布的表单时,由于没有模板显示消息,中间件会再次添加消息,因为中间件在消息发布时被调用,并且在重定向后再次被调用。

    我通过迭代中间件中的消息并检查我要添加的消息是否已经存在来解决这个问题。如果是,则不再添加。

  2. 当用户通过更新个人资料解决问题时,在下一个页面加载时,消息仍然存在。但在那之后,一切正常。在我的中间件的开头,我实际上做了一个检查,如果请求被发布,则返回 None (我本以为这会解决这两个问题,但它也没有解决)。

知道如何解决第二个问题吗?有没有更好的方法来解决我的第一个问题?

谢谢。

编辑:

有没有办法清除视图中的消息?我尝试迭代它们(没有 storage.used=False),它们仍然存在。我希望这能解决我的两个问题。

I have implemented a custom middleware that check for certain fields in a user's profile. The fields are not required at sign up (in order to make it quick and easy for a user to sign up), however, I would prefer that they fill them out.

My middleware checks if they are set. If not, it notifies the user via a message. I ran into two problems.

  1. Whenever I submit a posted form, because no template displays the messages, the middleware would add the message a second time since the middleware was called when the message was posted and after the redirect it was called again.

    I solved this problem by iterating through the messages in my middleware and checking if the message I am about to add is already in there. If yes, it doesn't add it again.

  2. When a user fixes the problem by updating their profile, on the very next page load, the messages are still there. After that though, everything works. At the beginning of my middleware, I actually put a check that returns None if the request was posted (I would have thought this would solve both problems, but it didn't solve either).

Any idea how to solve the 2nd issue? Is there a better way to solve my first one?

Thanks.

Edit:

Is there a way to clear the messages in a view? I've tried iterating through them (without storage.used=False) and they are still there. I would expect that this would solve both my problems.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

随波逐流 2024-11-14 19:28:14

那么,您是否使用 django.contrib.messages 来存储永久通知?它旨在显示一次性通知,即用户看到该消息一次,然后它就消失了。它的用途是诸如“表单编辑成功”之类的消息。

至于删除用户消息堆栈中的消息:任何时候您使用 RequestContext (已讨论 此处)渲染模板,所有消息都将被刷新(无论它们是否实际显示在页面上)。

我不完全确定这就是您真正想要的答案,而且我对您的问题有点困惑。但是,我有点确定您使用的消息超出了其预期目的,这可能就是您遇到麻烦的原因。

So, are you using django.contrib.messages to store permanent notices? It's intended for showing one-time notifications, where the user sees the message once and then it goes away. The type of things it's meant for are messages like, "Form edited successfully."

As far as getting rid of messages in a user's message stack are concerned: any time you use a RequestContext (discussed here) to render a template, all messages will be flushed (whether they are actually shown on the page or not).

I'm not totally sure this is the answer you're actually after, and I'm a bit confused by your question. However, I'm somewhat sure you're using messages outside its intended purpose, which is likely why you're running into trouble.

め可乐爱微笑 2024-11-14 19:28:14

您可以使用 process_reponse()中间件的方法添加消息。到那时,您将知道是否向用户显示消息,具体取决于他的个人资料现在是否已填写该字段。

考虑使用 django.contrib.messages。可能您不想在所有请求上显示“填写 XYZ 字段”消息,而只想在少数页面上显示“填写 XYZ 字段”消息,例如每当用户登录或查看其个人资料页面时。

You could use process_reponse() method of Middleware to add the message. By that time, you would know whether to show the message to user or not depending on whether his profile now has the field filled in.

Consider using django.contrib.messages. May be you do not want to show "fill in the XYZ field" message on all requests, but only on few pages, say whenever user logs in or views his profile page.

燃情 2024-11-14 19:28:14

只需将创建消息的逻辑放置到上下文处理器即可的中间件。修改请求并返回空字典。

Just place logic that creates messages to context processor instead of middleware. Modify request and return empty dict.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文