1

Basically, what I'm looking for is the Django/Python equivalent to the PHP

session_start(); print_R($_SESSION); 

That is, I'd like to dump all the information I currently have stored in the session so I can see what's in there.

2 Answers 2

3

The session is a dict-like object so you can simply print request.session to see what's in it.

The output will show up on the console of the server when using the development server - usually that's actually much better than print_r in PHP which dumps it to the webbrowser, next to whatever HTML you might have.

However, you could also return HttpResponse(repr(request.session)) if you do want to send it to the browser.

Sign up to request clarification or add additional context in comments.

5 Comments

What do you mean "The output will show up on the console of the server"? Are you saying I should use the REPL in the terminal? OR that it will appear in the console of the browser's developer tools?
Usually people use django-admin runserver during development. Anything you print in Python will show up there since print goes straight to stdout.
OCI. Thanks, bub!
There's no good reason to put the server into the background - but do consider using something like tmux so you can see its output and have a normal shell at the same time.
It's started up in it's own docker container, which I'm su-ing into with docker exec.
0

Since Django middleware stores all session data in request, we can access the session data in any view.As session is just a python dictionary(key,value pair),all the data can be accessed just like a normal python dictionary.

To store the data in session just do:

request.session['USER_NAME'] = 'John' 

Similarly, You can access it anywhere you have request object:

request.session.get('USER_NAME') 

So,To see the actual dictionary you can either print or log:

request.session 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.