Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One option might be to wrap Django's login/logout views with your own. For example:</p> <pre><code>from django.contrib.auth.views import login, logout def my_login(request, *args, **kwargs): response = login(request, *args, **kwargs) #fire a signal, or equivalent return response def my_logout(request, *args, **kwargs): #fire a signal, or equivalent return logout(request, *args, **kwargs) </code></pre> <p>You then use these views in your code rather than Django's, and voila.</p> <p>With regards to querying login status, it's pretty simple if you have access to the request object; simply check request's user attribute to see if they're a registered user or the anonymous user, and bingo. To quote the <a href="http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.user" rel="nofollow noreferrer">Django documentation</a>:</p> <pre><code>if request.user.is_authenticated(): # Do something for logged-in users. else: # Do something for anonymous users. </code></pre> <p>If you don't have access to the request object, then determining if the current user is logged in is going to be difficult.</p> <p><strong>Edit:</strong></p> <p>Unfortunately, you'll never be able to get <code>User.is_logged_in()</code> functionality - it's a limitation of the HTTP protocol. If you make a few assumptions, however, you might be able to get close to what you want.</p> <p>First, why can't you get that functionality? Well, you can't tell the difference between someone closing the browser, or someone spending a while on a page before fetching a new one. There's no way to tell over HTTP when someone actually leaves the site or closes the browser.</p> <p>So you have two options here that aren't perfect:</p> <ol> <li>Use Javascript's <code>unload</code> event to catch when a user is leaving a page. You'd have to write some careful logic to make sure you aren't logging out a user when they're still navigating <em>your</em> site, however.</li> <li>Fire the logout signal whenever a user logs in, just to be sure. Also create a cron job that runs fairly often to flush out expired sessions -- when an expired session is deleted, check that the session's user (if it's not anonymous) has no more active sessions, in which case you fire the logout signal.</li> </ol> <p>These solutions are messy and not ideal, but they're the best you can do, unfortunately.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload