简体   繁体   中英

How to authenticate user in Django Comment framework?

I'm using Django's comment framework as a part of my project. 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 ). 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.

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.

Thanks for the help!

Start from the documentation

Basically you need to (at least):

  1. enable django.contrib.auth in your settings.py
  2. define login view
  3. use @login_required decorator on the views you want restrict
  4. check if request.user.is_authenticated() in your form processing code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM