How to check whether a user is online in django template?

后端 未结 6 1043
终归单人心
终归单人心 2020-12-05 09:16

In template, when I use

{% if topic.creator.is_authenticated %}
Online
{% else %}
Offline
{% endif %}

the users turn out to be always onli

6条回答
  •  北海茫月
    2020-12-05 09:23

    Yes it does. But the correct way to check if a user is logged in or not is to use : request.user.is_authenticated. This will return True if the person is logged in other wise False.

    eg:

    in the template:

    {% if request.user.is_authenticated ==True %}
          do something awesome.
    

    in views pass request into the template.

    return render('url/filename.html',{'any variable name':request})
    

提交回复
热议问题