扩展会话中间件

时间:2012-03-06 05:08:34

标签: python django session storage middleware

我正在使用Django 1.3,我想修改会话中间件以添加一些可以帮助我的方法。

在./settings.py

SESSION_ENGINE='tcore.my_sessions.MySessionStore'

在./tcore/my_sessions.py中:

from django.contrib.sessions.backends.db import SessionStore
from django.contrib.sessions.middleware import SessionMiddleware
from django.conf import settings

class MySessionStore(SessionStore):    
    ....
    def my custom methods here

然而,当我这样做时,我会不断得到一些奇怪的例外。制作自定义会话引擎的正确方法是什么?

AttributeError at /create/
type object 'MySessionStore' has no attribute 'SessionStore'
Request Method: GET
Request URL:    http://localhost:8000/create/
Django Version: 1.3.1
Exception Type: AttributeError
Exception Value:    
type object 'MySessionStore' has no attribute 'SessionStore'

Python Version: 2.6.1

1 个答案:

答案 0 :(得分:3)

查看documentation of SESSION_ENGINE,举个例子:django.contrib.sessions.backends.file。该模块的源代码定义了SessionStore类。这就是你应该做的事情:

./ TCORE / my_sessions.py:

from django.contrib.sessions.backends.db import SessionStore as DbSessionStore

class SessionStore(DbSessionStore):
    def __init__(self, *args, **kwargs):
        print 'hello from SessionStore'
        super(SessionStore, self).__init__(*args, **kwargs)

settings.py:

SESSION_ENGINE='tcore.my_sessions'