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

4

1 回答 1

0

文档

SessionMiddleware被激活时,每个HttpRequest对象——任何 Django 视图函数的第一个参数——都会有一个session 属性,它是一个类似字典的对象。

因此,每次您提出请求时 - 如果会话尚不存在,则会创建会话。何时context_processor应用 - 会话已存在。你不应该担心这一点。

于 2012-10-22T13:18:09.970 回答