V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  artlearn  ›  全部回复第 1 页 / 共 1 页
回复总数  4
2015-09-03 16:25:13 +08:00
回复了 artlearn 创建的主题 Django 请问 django 视图类中如何获取 request
View 类 原来代码
class View (object ):
"""
Intentionally simple parent class for all views. Only implements
dispatch-by-method and simple sanity checking.
"""

http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace']

def __init__(self, **kwargs ):
"""
Constructor. Called in the URLconf; can contain helpful extra
keyword arguments, and other things.
"""
# Go through keyword arguments, and either save their values to our
# instance, or raise an error.
for key, value in six.iteritems (kwargs ):
setattr (self, key, value )

@classonlymethod
def as_view (cls, **initkwargs ):
"""
Main entry point for a request-response process.
"""
# sanitize keyword arguments
for key in initkwargs:
if key in cls.http_method_names:
raise TypeError ("You tried to pass in the %s method name as a "
"keyword argument to %s (). Don't do that."
% (key, cls.__name__))
if not hasattr (cls, key ):
raise TypeError ("%s () received an invalid keyword %r. as_view "
"only accepts arguments that are already "
"attributes of the class." % (cls.__name__, key ))

def view (request, *args, **kwargs ):
self = cls (**initkwargs )
if hasattr (self, 'get') and not hasattr (self, 'head'):
self.head = self.get
self.request = request #就在这里
self.args = args
self.kwargs = kwargs
return self.dispatch (request, *args, **kwargs )
2015-09-03 16:18:26 +08:00
回复了 artlearn 创建的主题 Django 请问 django 视图类中如何获取 request
真诚感谢 @aggron
让 BaseMixin 继承 View 类
然后 self.request 就可以
遇到这个问题的新手朋友可以这样做。
2015-09-03 15:33:20 +08:00
回复了 artlearn 创建的主题 Django 请问 django 视图类中如何获取 request
这里的人一个个逼格真的好高,我自己解决
2015-09-03 08:22:13 +08:00
回复了 artlearn 创建的主题 Django 请问 django 视图类中如何获取 request
谢谢回答
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5631 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 14ms · UTC 03:35 · PVG 11:35 · LAX 19:35 · JFK 22:35
Developed with CodeLauncher
♥ Do have faith in what you're doing.