Extending the Session Middleware

五迷三道 提交于 2019-12-08 01:36:22

问题


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'

In ./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

回答1:


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:

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'


来源:https://stackoverflow.com/questions/9578204/extending-the-session-middleware

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!