[Answered ]-Is there any way to get login-user information in javascript (Django)?

2πŸ‘

I assume you are using session authentication.

You can do it in several ways.

First, you can add an extra field in your comments API by doing something like this:

comments['editable'] = request.user.user_name == comment.author_name

If it is True, user can edit the comment and vice versa. I believe this is a better way.

Second way is directly in your script get access to django user and compare there:

<script>
   var editable = comment.author_name == {{ request.user.user_name }}
</script>

This is not a good approach. Because you are mixing django variable with jquery! If your script is in another file, it won’t work at all.

So, I would go with the first way.

Hope it helps

0πŸ‘

In your templates:

<script>
    var reply_name = {{ request.user.user_name }};
    get_comment(repay_name) #call what you want using js
</script>
πŸ‘€Windsooon

Leave a comment