Friday, January 30, 2015

Re: How to detect (daily) User Login when using cookies?

On Fri, Jan 30, 2015 at 1:50 PM, Collin Anderson <> wrote:
> Hi,
> If you use a custom authentication backend, you could update it every time
> get_user(request) is called.

HTTP is stateless, authentication happens every request, so that gets
called on every request, causing session modification on each request.

How about:

from django.utils import timezone

class DailyLoginMiddleware(object):
def process_request(self, request):
if request.user.is_authenticated():
today ='%Y%m%d')
if request.session.get('last_seen') != today:
request.session['last_seen'] = today
setattr(request, 'new_today', True)

Store todays date in the session, check to see if it is changed, only
modify the session if the date has changed, set an attribute on the
request so that later views can include that information.



You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
To view this discussion on the web visit
For more options, visit

No comments:

Post a Comment