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 7077917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:28:56+00:00 2026-05-28T06:28:56+00:00

How can I do this request.session[‘key’] = ‘value’ for the user which user_id is

  • 0

How can I do this

request.session['key'] = 'value'

for the user which user_id is 47?

Keep in mind that I’m not currently logged in with that user, I want to do it in shell.

  • 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-05-28T06:28:57+00:00Added an answer on May 28, 2026 at 6:28 am

    See the section of the Session docs entitled “Using sessions out of views”.

    The problem though is that Django doesn’t store the user with the session (by design, for security purposes). So the only way to retrieve a session is through it’s key. That key is stored with the user’s client and passed to the server to associate the session with the logged in user. In other words, you’re going to have a hard time determining which session belongs to which user.

    More to the point, the session data is actually encrypted in the database as well, so there’s not even any way to query directly for the user id stored in it. The following will work, but you’ll have to query each session one by one to get the right user. Depending on how many sessions your database currently has, this could be extremely expensive. Mark as USE AT YOUR OWN RISK

    from django.contrib.sessions.models import Session
    from django.contrib.sessions.backends.db import SessionStore
    
    for session in Session.objects.all():
        data = SessionStore().decode(session.session_data)
        if data.get('_auth_user_id') == user_id_you_want:
            user_session = SessionStore(session_key=session.session_key)
            # you can modify the session data here like normal, then:
            user_session.save()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm aware that you can get session variables using request.session['variable_name'] , but there doesn't
Right now I can use this URL to request a Google Static Maps image
I know you can do this <%= Request.Form[0] %> But how do you do
how can i use request.querystring in asp.net.i have a linkbutton and this is in
can I make my own headers in HTTP request ? e.g. This is normal
Can this be done by setting a property? I'd prefer that approach then to
how can I do this in express? req.session.oa = new OAuth(....); // and in
Suppose I do this: request.session['x'] = 33 How do I make this session variable
i use this code : request.session.set_test_cookie() the all code is : def main(request, template_name='index.html'):
This first script gets called several times for each user via an AJAX request.

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.