Friday, October 2, 2015

Re: Method call at end of Django session

Hi,

I don't think you can reliably do that. Django never knows when the user closes his browser window. Because a session is stateless (there is only a connection to the server when it responds to a request). However you could do this via a bit of ajax or a pretty unreliable javascript event.

What I would do is to ping the server via javascript once every minute or so - you then update a timestamp on a session object or the user model or something like that. Then if the timestamp is older than 5 minutes, you can safely delete the temporary files.

Another way would be to add an event handler to the onbeforeunload event, see http://stackoverflow.com/questions/20853142/trying-to-detect-browser-close-event. When the event fires, you just fire an ajax call to your server that deletes the files.

HOWEVER, I know that this doesn't always fire and can be a bit tricky. But it's worth a shot.

Regards,

Andréas

2015-10-02 18:44 GMT+02:00 Amarjeet Kapoor <amarjeet.kapoor1@gmail.com>:
Want to know how and where can I implement a method in Django which is
to called automatically only when a session end ( i.e. when user close
the webapp or browser ). I want to know where that method should be
written, how to call it automatically  after end of session.

I want to use this method to delete some user specific files created
after background processing in my webapp.


--
Amarjeet Singh
https://amarjeetkapoor1.wordpress.com
https://github.com/amarjeetkapoor1
https://bitbucket.org/amarjeetkapoor

--
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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CABskhPPQV%2BPXe-ObEbb05Bk7KpbLcv1XB__jhx2d-Q54qBzSZQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

--
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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CALXYUbmB-26TtmX6Ughi4mE3SKKVneFLWiJyumP6jGf-byE4pg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment