Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7751245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:29:53+00:00 2026-06-01T11:29:53+00:00

I’ve got some nifty code that on user login, finds any other user logged

  • 0

I’ve got some nifty code that on user login, finds any other user logged in as that user and logs them out. It works just fine, but it doesn’t offer any indication as to why these people are no longer logged in, so I’m trying to remedy that with Django’s messaging system.

You’ve been logged out because somebody has been sharing their login credentials.
Don’t do that, it’s uncool.

The only problem is that message.error() (understandably) requires request as the first argument, and at the point at which I’m kicking other users, I don’t have their request objects, I only have the kicking user’s object.

So I’m working on options. I suppose I could write a model to log these sorts of messages, keyed by session id (which I have from every user), and then some middleware that checks the db for any messages for that session id on every request but that seems really wasteful, not to mention anti-DRY. There must be a better way to do it, but I don’t know what it is yet.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:29:54+00:00Added an answer on June 1, 2026 at 11:29 am

    I appear to have solved this in a rather round-about, hackish sort of way. I’ll post it here in case anyone else comes looking with a similar problem.

    First I had to set the storage backend to use sessions rather than whatever the default is. I thought the default was sessions, but from inspecting the database, I didn’t see it.

    # settings.py
    MESSAGE_STORAGE = 'django.contrib.messages.storage.session.SessionStorage'
    

    Next, I added a handler to the user.is_logged_in signal:

    def kick_my_other_sessions(sender, request=None, user=None, **kwargs):
    
        from django.contrib.messages import constants
        from django.contrib.messages.storage.base import Message
        from django.contrib.sessions.backends.db import SessionStore
        from django.contrib.sessions.models import Session
    
        from tracking.models import Visitor
    
        keys = [v.session_key for v in Visitor.objects.filter(user=request.user).exclude(session_key=request.session.session_key)]
        for session in Session.objects.filter(session_key__in=keys):
    
            s = SessionStore(session.session_key)
    
            # Hack to log the user out without expiring the session immediately,
            # otherwise they'll never see their message
            s["_auth_user_id"] = 0
    
            # The _messages property is a list of Message objects
            s["_messages"] = [Message(constants.ERROR, "You've been logged out because someone else has logged into your account from another computer.")]
    
            s.save()
    

    And that’s it. Originally, I just deleted the session, but this (admittedly hackish) way, I can log out the user and let them retain the session so they can see their message.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.