最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Django : How to access current logged in user's id in javascript? - Stack Overflow

programmeradmin2浏览0评论

I have defined a get_queryset method in a viewset.

class BooksViewSet(ReadOnlyModelViewSet):
    serializer_class = BooksSerializer
    permission_classes = (IsAuthorizedToAccess, )

    def get_queryset(self):
        queryset = Books.objects.all();
        return queryset

Using javascript i want to display books of current user on one side and books of other all users on right side. I did all the code but could not access the current user id in my js file.

if(auther.id == "current user id")
{
}

I Want to access the user id like this. For all search i did , i got to know about adding this user id as a hidden field or passing this id .But i cant as the method only allows to return the queryset object.

Can anyone please help me understand this.

EDIT:MY SOLUTION

In my views , i define the get_context_data method.

 def get_context_data(self, *args, **kwargs):
    ctx = super(class_name, self).get_context_data(*args, **kwargs)
    ctx["author_id"]=self.request.user.id
    return ctx

In my template,i defined a hidden field:

<input type="hidden" id="userId" value={{author_id}}>

And in my JS:

var x= document.getElementById("author_id").value;

I have defined a get_queryset method in a viewset.

class BooksViewSet(ReadOnlyModelViewSet):
    serializer_class = BooksSerializer
    permission_classes = (IsAuthorizedToAccess, )

    def get_queryset(self):
        queryset = Books.objects.all();
        return queryset

Using javascript i want to display books of current user on one side and books of other all users on right side. I did all the code but could not access the current user id in my js file.

if(auther.id == "current user id")
{
}

I Want to access the user id like this. For all search i did , i got to know about adding this user id as a hidden field or passing this id .But i cant as the method only allows to return the queryset object.

Can anyone please help me understand this.

EDIT:MY SOLUTION

In my views , i define the get_context_data method.

 def get_context_data(self, *args, **kwargs):
    ctx = super(class_name, self).get_context_data(*args, **kwargs)
    ctx["author_id"]=self.request.user.id
    return ctx

In my template,i defined a hidden field:

<input type="hidden" id="userId" value={{author_id}}>

And in my JS:

var x= document.getElementById("author_id").value;

Share Improve this question edited Jan 11, 2016 at 12:35 siddaqui asked Jan 11, 2016 at 10:30 siddaquisiddaqui 1391 gold badge1 silver badge7 bronze badges 7
  • I think you could add the user id to the context – Jonas Grumann Commented Jan 11, 2016 at 10:37
  • @JonasGrumann Can you please explain what do you mean by context. – siddaqui Commented Jan 11, 2016 at 10:39
  • 1 In the first example he's adding a number to the context, so it will be available in the template: docs.djangoproject.com/en/1.9/ref/class-based-views/… – Jonas Grumann Commented Jan 11, 2016 at 10:42
  • Is it ok to use get_queryset and get_context_data methods together in a ReadOnlyModelViewSet – siddaqui Commented Jan 11, 2016 at 10:46
  • @JonasGrumann thank you so much. – siddaqui Commented Jan 11, 2016 at 12:31
 |  Show 2 more comments

3 Answers 3

Reset to default 24

As of Django 2.1, a new built in template tag has been introduced specifically for this use case: json_script.

https://docs.djangoproject.com/en/dev/ref/templates/builtins/#json-script

The new tag will safely serialize template values and protects against XSS.

It is not necessary to set any context variables as the request.user is already included in every view:

template.html

{{ request.user.id|json_script:"user_id" }}

script.js

const user_id = JSON.parse(document.getElementById('user_id').textContent);

You can use template tag as well. Like {{author.id}} You can also assign to a javascript variable the value and then use it

var id = {{ author.id }}

If the javascript is in your template file, you can access it like any other like this:

{% for author in authors %}
    if('{{author.id}}' == "current user id")
    {
    }

{% endfor %}
发布评论

评论列表(0)

  1. 暂无评论