扩展 Django 评论系统

发布于 2024-07-27 04:35:02 字数 405 浏览 3 评论 0原文

这就是我希望在我的网站上发表评论的方式:

  • 如果用户登录,他们会看到一个“添加评论”框,其中有一个用于评论文本的文本字段,以及一个复选框,如果选中则将匿名发布评论
  • 如果用户未登录,它将向用户显示上述所有内容,以及一个额外的文本字段,用户可以在其中输入显示名称。

所以基本上,如果用户登录,他们可以匿名发表评论或使用他们的用户名发表评论。 如果他们没有登录,他们可以匿名发帖,也可以当场编造一个名字,仅供显示之用。

我该怎么做呢? 我已经设置了模板,该模板根据用户的登录状态向用户显示适当的文本框和复选框。

我考虑了 django.contrib.comments.views.post_comment 的包装函数,但该视图只接受一个不可变的请求对象。 你会怎么做呢?

This is how I want commenting to work on my site:

  • if the user is logged in, they are presented with an "add comments" box which has a text field for the comment text, and a checkbox that will post the comment anonymously if checked
  • if the user is not logged in, it will present the user with everything above, plus an extra text field where they can enter a display name.

SO basically if the user is logged in they can ither comment anonymously or with their username. If they aren't logged in, they can either post anonymously or they can make up a name on the spot for display purposes only.

How can I go about doing this? I already have the template set up which presents the user with the appropriate textboxes and checkboxes depending on their logged in status.

I thought about a wrapper function around django.contrib.comments.views.post_comment, but that view only takes a request object, which is immutable. How would you go about this?

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

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

发布评论

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

评论(2

手长情犹 2024-08-03 04:35:02

听起来您可能需要一个自定义评论应用程序 - 查看 文档。 这是另一个示例(这个添加了验证码到标准评论表)。 看起来是一个非常简单的过程:)

Sounds like you might be needing a custom comments app -- check out the docs. Here's another example (this one adds a CAPTCHA to the standard comment form). Seems like a pretty straightforward process :)

影子是时光的心 2024-08-03 04:35:02

只需抄袭 django.contrib.comments.views.post_comment 中的内容即可编写您的视图,但更适合您的用例。 使用支持您的特定 UI(在演示和验证方面)的表单。 看起来电子邮件字段不可为空,因此如果不可用,则必须输入一个虚拟值。

如果您能够包装现有的功能,那就太好了,但我认为这不实用。 为了使包装方法发挥作用,现有代码在编写时必须考虑到可包装性 - 但这种情况尚未发生。

Just write your view cribbing what you can from django.contrib.comments.views.post_comment but more suited to your use case. Use a form which supports your specific UI (in terms of presentation and validation). It looks as if the email field is not nullable so you will have to put a dummy value in if one is not available.

It would have been nice if you could have wrapped the existing functionality, but I don't think that's practical. For a wrapping approach to work, the existing code would have had to be written with wrappability in mind - and that hasn't happened.

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