扩展会话中间件 [英] Extending the Session Middleware

查看:146
本文介绍了扩展会话中间件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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


推荐答案

查看 SESSION_ENGINE的文档,举个例子: 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'

这篇关于扩展会话中间件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆