0

In part of my application i have some custom settings for visitors. When user load page i try to check custom settings for request and if settings exist i do customization in page.

i do it in Template Context Processor

def shared_values(request):

    show_help = request.session.get('show_help', True)
    return {
        'show_help': show_help,
    }

It's work perfect, but i have problem - each time, when i check session django create new entry in Session table if entry does not exist before.

How can i check session without creating new one?

Edit 1:

Then i disable checking for session key, i see what django does not create any Session entry for requests.

  1. I login as admin in 1st browser (chrome) and delete all session (except one for mydself)
  2. Edit context processor, so i don't access sessions in my project:

    show_help = True

  3. Open page in different browser (firefox)

  4. Switch to 1st browser and check if new session object is appear, but nothing has changed - only one session object exist.

Edit 2:

i use django 1.4.1

Sergey Lyapustin
  • 1,917
  • 17
  • 27

1 Answers1

0

Docs:

When SessionMiddleware is activated, each HttpRequest object – the first argument to any Django view function – will have a session attribute, which is a dictionary-like object.

So every time you make request - session will be created, if it does not exists already. When context_processor is applied - session already exists. You should not worried about that.

Serhii Holinei
  • 5,758
  • 2
  • 32
  • 46
  • Actually this is not true. Because then i disable checking for session key, i see what django does not create any Session entry. – Sergey Lyapustin Oct 22 '12 at 13:53
  • I made an experiment. I removed all session entries in database and set a breakpoint at first line of first context_processor. I run debug , process stoped at breakpoint, I looked at database and... voila - there is new session entry. Maybe you should make more detailed description of the problem you have – Serhii Holinei Oct 23 '12 at 07:43
  • What version of django do you have? – Serhii Holinei Oct 23 '12 at 09:15
  • Django 1.4.1, on 1.4.2 i have the same result. – Sergey Lyapustin Oct 23 '12 at 09:43