问题描述
我正在使用Django 1.3,我想修改会话中间件,以添加一些可以帮助我的方法。
I'm using Django 1.3, and I'd like to modify the session middleware to add some methods that will help me out.
In ./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
但是,当我这样做时,我会不断得到一些奇怪的例外。制作自定义会话引擎的正确方法是什么?
However, I keep getting some weird exceptions when I do this. What's the proper way to make a custom Session Engine?
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
推荐答案
查看,举个例子: django.contrib.sessions.backends.file
。该模块的源代码定义了一个SessionStore类。所以这就是你应该做的:
Looking at the documentation of SESSION_ENGINE, take such an example: django.contrib.sessions.backends.file
. The source of this module defines a SessionStore class. So that's what you should do too:
./ tcore / my_sessions.py:
./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:
settings.py:
SESSION_ENGINE='tcore.my_sessions'
这篇关于扩展会话中间件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!