DRFSession

编程

SELECT `django_session`.`session_key`, `django_session`.`session_data`, `django_session`.`expire_date` FROM `django_session` WHERE (`django_session`.`session_key` = "wnzlmvljlpsugmdhi92podf13u6zt1ii" AND `django_session`.`expire_date` > "2019-11-18 10:19:33.320862"); args=("wnzlmvljlpsugmdhi92podf13u6zt1ii", "2019-11-18 10:19:33.320862")

settings.py 中间件

MIDDLEWARE = [

"django.middleware.security.SecurityMiddleware",

"django.contrib.sessions.middleware.SessionMiddleware", # 执行sql

"django.middleware.common.CommonMiddleware",

"django.middleware.csrf.CsrfViewMiddleware",

"django.contrib.auth.middleware.AuthenticationMiddleware",

"django.contrib.messages.middleware.MessageMiddleware",

"django.middleware.clickjacking.XFrameOptionsMiddleware",

]

django.contrib.sessions.base_session.AbstractBaseSession

class AbstractBaseSession(models.Model):

session_key = models.CharField(_("session key"), max_length=40, primary_key=True)

session_data = models.TextField(_("session data"))

expire_date = models.DateTimeField(_("expire date"), db_index=True)

objects = BaseSessionManager()

class Meta:

abstract = True

verbose_name = _("session")

verbose_name_plural = _("sessions")

def __str__(self):

return self.session_key

@classmethod

def get_session_store_class(cls):

raise NotImplementedError

def get_decoded(self):

session_store_class = self.get_session_store_class()

return session_store_class().decode(self.session_data)

当cli发起请求时到达视图函数前,执行中间件process_reqeust

SESSION_ENGINE = "django.contrib.sessions.backends.db"

class SessionMiddleware(MiddlewareMixin):

def __init__(self, get_response=None):

self.get_response = get_response

engine = import_module(settings.SESSION_ENGINE)

self.SessionStore = engine.SessionStore

def process_request(self, request):

session_key = request.COOKIES.get(settings.SESSION_COOKIE_NAME) #1.从cookie获取session_Key

request.session = self.SessionStore(session_key) #

以上是 DRFSession 的全部内容, 来源链接: utcz.com/z/512524.html

回到顶部