Py学习  »  问与答

请问django中的类视图如何使用session呢?如何引用request?

无情v双星-weibo • 8 年前 • 2318 次点击  

如题所示,求各位大神指导,谢谢了

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/1308
 
2318 次点击  
文章 [ 1 ]  |  最新文章 8 年前
rapospectre
Reply   •   1 楼
rapospectre    8 年前

直接使用就可以撒,你的类至少继承了django.views.generic.view,最基本的一个类,这个类里有dispatch函数负责方法分发:

def dispatch(self, request, *args, **kwargs):
# Try to dispatch to the right method; if a method doesn't exist,
# defer to the error handler. Also defer to the error handler if the
# request method isn't on the approved list.
if request.method.lower() in self.http_method_names:
    handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
else:
    handler = self.http_method_not_allowed
return handler(request, *args, **kwargs)

里面return的request直接拿来用就行:

def get(self, request, *args, **kwargs):
     pass

session直接request.session,和fbv使用方法一样。