Django 的评论表单应该知道用户何时登录吗?

发布于 2024-09-25 13:33:04 字数 305 浏览 1 评论 0原文

我在我的项目中使用标准 Django 评论应用程序。如果用户已登录,则评论表单仍显示 user_name 和 user_email 字段。如果在其中输入任何内容,数据将与注释以及登录用户的 user_id 一起保存到数据库中。

显示评论时,显示的是登录用户的名称,而不是在评论表单中输入的用户名。

如果用户登录,我预计 user_name 和 user_email 字段不会显示在表单上,​​因为它们在这种情况下毫无意义。难道我做错了什么就应该这样吗?如果这是奇怪的标准行为,那么当用户登录时隐藏这些字段(或使用登录用户的姓名/电子邮件)的最简单方法是什么?谢谢。

I'm using the standard Django comments app in my project. If the User is logged in then the comment form still shows fields for user_name and user_email. If anything is entered into those, the data is saved to the database with the comment, along with the user_id of the logged-in User.

When the comment is displayed, the name of the logged-in User is shown, rather than the user_name entered into the comment form.

I would have expected the user_name and user_email fields to not be shown on the form if the User is logged in, as they're pointless in this situation. Is it supposed to behave like that way and I've done something wrong? If this is, bizarrely, standard behaviour, what's the very simplest way to hide these fields (or use the logged-in User's name/email) when the User is logged in? Thanks.

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

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

发布评论

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

评论(1

小红帽 2024-10-02 13:33:04

请记住,通用 Django 应用程序被设计为……通用。他们不一定处理所有特殊情况。

如果您想更改此表单,您应该编写自己的模板。在模板中,您可以使用 !user.is_authenticated 将字段添加到表单中(确保您使用的是 RequestContext)。

Keep in mind that generic Django apps are designed to be, well.. generic. They don't necessarily handle all the special cases.

If you want to change this form, you should write your own template. In the template, you can use !user.is_authenticated to add fields to the form (make sure you are using RequestContext).

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