简体   繁体   English

如何在Django Comment框架中对用户进行身份验证?

[英]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): 基本上,您需要(至少):

  1. enable django.contrib.auth in your settings.py settings.py启用django.contrib.auth
  2. define login view 定义登录视图
  3. use @login_required decorator on the views you want restrict 在要限制的视图上使用@login_required装饰器
  4. check if request.user.is_authenticated() in your form processing code. 检查表单处理代码中的if request.user.is_authenticated()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 django rest 框架对用户进行身份验证 - How to authenticate a user using django rest framework 如何在Django rest框架中对用户进行身份验证? - how to authenticate users in Django rest framework? 如何通过 django rest 框架中的其他微服务对属于另一个数据库的用户进行身份验证? - How can I authenticate a user who belongs to another database through my other microservice in django rest framework? 无法使用django rest框架进行身份验证(request.user = AnonymousUser) - Not able to authenticate using the django rest framework (request.user = AnonymousUser) 如何以编程方式对 Django 中的用户进行身份验证? - How can I programmatically authenticate a user in Django? 如何从另一个应用程序验证Django用户 - How to Authenticate a Django user from another application 如何在 django 中验证来自 mysql 的用户? - How to authenticate User from mysql in django? 如何在Django中验证有效的超级用户 - How to authenticate valid super user or not in django 如何在 Django 中使用自定义 model 对用户进行身份验证? - How to authenticate user using custom model in Django? 登录用户如何评论帖子? 姜戈 - How can a logged in user comment on a post? django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM