1

I have installed django-redis-session via pip and add this settings to the settings.py file:

SESSION_ENGINE = 'redis_sessions.session'

When I go to the terminal and type:

$ redis-cli monitor

I can see there are changes when I log in and log out (using python-social-auth).

My question is: How can I get the session (from redis) based on the user?

If I call is_authenticated() User's function it works, but my DB's (PostgreSQL) Session table has 0 rows.

Fianlly, is there any way to write the session key and data manually? I will need to use a complex PK for session based in two fields stored in redis.

Gocht
  • 9,924
  • 3
  • 42
  • 81
  • 1
    I don't understand your question. Why would you expect any rows in Posgres? You've specifically set the session backend to redis. – Daniel Roseman Mar 02 '16 at 14:41
  • @DanielRoseman Yes, I don't expect rows in Postgres I was just saying that session has been succesfully stored in redis. – Gocht Mar 02 '16 at 14:43

1 Answers1

2

You should be able to get the current session with request.session. This should work, whichever session backend you use, whether or not the user is logged in.

See the docs on using sessions in views for more info.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • From `request.session` I can access to user, thanks. – Gocht Mar 02 '16 at 15:32
  • 1
    If you need the logged in user, then use `request.user` - you shouldn't have to go via the session. – Alasdair Mar 02 '16 at 15:55
  • Thanks again, one more question: Do you know how can I write the session manually? I need to use a complex PK for sessions and I'd like to store this in redis. – Gocht Mar 02 '16 at 16:03
  • I'm not sure what you mean by 'write the session manually'. You can store values in the session with `request.session['key'] = 'value'`. If you are trying to change the session key, then I'm not immediately sure how you would do that, and I don't think it's a good idea anyway. Session keys should not be guessable. – Alasdair Mar 02 '16 at 16:08
  • I will take a look. Thanks again! – Gocht Mar 02 '16 at 16:09