One option might be to combine the entry / exit views in Django with your own. For example:
from django.contrib.auth.views import login, logout def my_login(request, *args, **kwargs): response = login(request, *args, **kwargs)
Then you use these views in your code, not in Django and voila.
Regarding the request for login status, it is quite simple if you have access to the request object; just check the request user attribute to see if they are a registered user or anonymous user, as well as bingo. To quote the Django documentation :
if request.user.is_authenticated(): # Do something for logged-in users. else: # Do something for anonymous users.
If you do not have access to the request object, determining whether the current user will be registered will be difficult.
Edit:
Unfortunately, you can never get the functionality of User.is_logged_in() - this is a limitation of the HTTP protocol. However, if you make a few assumptions, you can get closer to what you want.
First, why can't you get this functionality? Well, you canโt tell the difference between someone who closes the browser, or someone is wasting time on the page before choosing a new one. It is not possible to send an HTTP message when someone really leaves the site or closes the browser.
So, you have two options that are not ideal:
- Use Javascript
unload to catch when the user leaves the page. You should write some careful logic to make sure you are not logged out when the user is still moving your site. - Fire the exit signal when the user logs in to be sure. Also, create a cron task that runs quite often to clear expired sessions - when deleting an expired session, make sure that the session user (if it is not anonymous) does not have more active sessions, in which case you start a logout signal.
These decisions are messy and not perfect, but unfortunately, this is the best you can do.
ShZ Jan 02 '09 at 3:25 2010-01-02 03:25
source share