[英]How to authenticate user in Django Comment framework?
I'm using Django's comment framework as a part of my project. 我将Django的注释框架用作项目的一部分。 With default settings, anonymous users can make comments at will.
使用默认设置,匿名用户可以随意发表评论。
I want to change this feature such that only authenticated users can post comments. 我想更改此功能,以便只有经过身份验证的用户才能发表评论。 Moreover, I want this authenticated user's name to show up next to the comment.
此外,我希望该经过身份验证的用户名显示在评论旁边。
How do I go about doing so? 我该怎么做? I've read up on the documentation, and I understand the pre-defined comment model has a "user" field which is a ForeignKey to the User model / user who posted the comment ( Link here ).
我已经阅读了文档,并且了解了预定义的注释模型有一个“用户”字段,该字段是发布该注释的用户模型/用户的ForeignKey( 链接在此处 )。 However, I don't understand how to assign request.user (ie the current authenticated user) to this user field that belongs to the instance of the comment.
但是,我不明白如何将request.user(即当前经过身份验证的用户)分配给属于注释实例的此用户字段。
In other words, how does Django process the form data on the front-end to the Comment model in the back-end, and how can I edit this process such as to assign request.user to the user field in the comment model. 换句话说,Django如何处理前端的表单数据到后端的Comment模型,以及如何编辑此过程,例如将request.user分配给注释模型中的user字段。
Thanks for the help! 谢谢您的帮助!
Start from the documentation 从文档开始
Basically you need to (at least): 基本上,您需要(至少):
django.contrib.auth
in your settings.py
settings.py
启用django.contrib.auth
@login_required
decorator on the views you want restrict @login_required
装饰器 if request.user.is_authenticated()
in your form processing code. if request.user.is_authenticated()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.