Django 中经过身份验证的评论
我想在 Django 中实现经过身份验证的评论。我在网上搜索了一些答案,包括一些关于 SO 的答案,但它们大多来自 2008 年和 2009 年,关于 Django 1.1 左右。我目前使用的是 Django 1.3。我想知道 Django 1.3 中是否有一个优雅的解决方案。
如果用户未经过身份验证,我可以隐藏模板中的评论表单,我就知道了。
我该如何处理视图?我读了一些 文章 展示了如何处理视图,包括预填充表单并使用一些 try- except 块来确保传入的用户数据正确,但它们看起来很漂亮 hacky。 Django 1.3 现在有更好的方法吗?
顺便说一句,我在我的应用程序中实现了一个非常简单的注册系统。我不要求名字、姓氏等。只要求用户名、电子邮件(甚至是假的)和密码。所以我的用户在数据库中只有用户名、电子邮件和密码。
谢谢!
I'd like to implement authenticated commenting in Django. I've searched the net and found a few answers, including a few here on SO, but they are mostly from 2008 and 2009, on Django 1.1 or so. I'm currently on Django 1.3. I'm wondering if there's an elegant solution in Django 1.3.
I can hide the comment form in the template if user is not authenticated, that much I know.
How do I deal with the view? I read a few articles that show how to deal with the view, including pre-populating the form and using some try-except blocks to make sure the incoming user data is correct, but they seem pretty hacky. Is there a better way now in Django 1.3?
Btw I implement a very simple registration system in my app. I don't ask for first name, last name, etc etc. Just a username, an email (even a fake one), and a password. So my Users will have only username, email, and password in the database.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于任何想知道的人来说,事实证明,在 Django 1.3 中,您在提交表单时无需担心用户模型字段。在您的表单中,只需包含
form.comment
和隐藏字段 object_pk、content_type 和时间戳,如果用户已经登录,则将自动处理其他字段(如名字等) 。For anyone that's wondering, it turns out that in Django 1.3 you need not worry about the User model fields when submitting a form. In your form, simply have
form.comment
and the hidden fields object_pk, content_type, and timestamp, and if the user is logged in already the other fields like first name, etc. will be taken care of automatically.