artlearn

artlearn

V2EX 第 70403 号会员,加入于 2014-08-10 02:23:09 +08:00
解决问题的终极方案——大事化小小事化无
artlearn 最近回复了
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   ·   实用小工具   ·   2604 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 11ms · UTC 11:34 · PVG 19:34 · LAX 03:34 · JFK 06:34
Developed with CodeLauncher
♥ Do have faith in what you're doing.