在视图函数中直接可以将 request 作为参数很简单。
但在视图类中怎么获取呢?
class BaseMixin (object ):
def get_context_data (self,*args,**kwargs ):
context = super (BaseMixin,self ).get_context_data (*args,**kwargs )
try:
#热门文章
context['hot_post_list'] = Article.objects.order_by ("-views")[0:10]
#导航条
context['nav_list'] = Category.objects.filter (status=1 )
#最新文章
context['latest_post_list'] = Article.objects.order_by ("-create_time")[0:10]
#用户发布文章数
#if request.user.is_authenticated ():#验证登录,这里需要 request ,提示没有 request 模块,不知道用什么方式获取 request
#context['pushed_count'] = Pushed.objects.filter (user_id=request.user.id ).count ()#要获取登录用户 ID
except Exception as e:
logger.error (u'加载通用信息出错'+''.join (e ))
return context
知道的直接告诉我代码吧,别让我看文档了,我要是有那个心思去看文档早不提问了。
1
guoqiao 2015-09-03 07:21:10 +08:00
哈哈, 又见理直气壮的拿来主义.
|
2
aggron 2015-09-03 07:35:00 +08:00 1
用 self.request
在 class based views 中是这样的 class FooView (BaseMixin, View ): 虽然 BaseMixin 对象没有 request 属性,但 View 对象有,所有就可以使用,这算很常见的 Mixin 模式(多继承),你可以看看 Python MRO 的知识 |
3
hpeng 2015-09-03 07:48:06 +08:00 via Android
滚,希望您将我拉黑,别让我去 block 您了,有那心思我就不回复您了
|
4
artlearn OP 谢谢回答
|
5
roychan 2015-09-03 08:38:34 +08:00
「我根本不是搞程序的,请原谅我的问题很白痴,能答就答不能答你就闪。大路朝天各走各边,你走你阳关道,我过我的独木桥。我只需要那些好心人回答,根本没把你放在眼里,所以你也不需要把我放在眼里,回滚第一句。别给我装逼拿来主义,除非你这辈子没用过开源。你很屌就不要混社区。回滚第一句。」
|
6
sorcerer 2015-09-03 10:20:58 +08:00 via iPhone
楼主这签名,没人会回答你的
|
7
jakes 2015-09-03 10:38:57 +08:00 via Android
真他喵理直气壮
|
8
KIDJourney 2015-09-03 12:52:16 +08:00
不可以举报吗。。。
|
9
aisk 2015-09-03 13:43:05 +08:00
为社区做点好事,千万别回答楼主。
为社区做点好事,千万别回答楼主。 为社区做点好事,千万别回答楼主。 |
10
aisk 2015-09-03 13:44:28 +08:00
|
11
aisk 2015-09-03 13:44:36 +08:00
|
12
BGLL 2015-09-03 14:55:25 +08:00 via Android
楼主简介太 66666 了:
“我根本不是搞程序的,请原谅我的问题很白痴,能答就答不能答你就闪。大路朝天各走各边,你走你阳关道,我过我的独木桥。我只需要那些好心人回答,根本没把你放在眼里,所以你也不需要把我放在眼里,回滚第一句。别给我装逼拿来主义,除非你这辈子没用过开源。你很屌就不要混社区。回滚第一句。” |
13
zhuangzhuang1988 2015-09-03 14:58:59 +08:00
很简单啊, 给点钱就好了.
|
14
artlearn OP 这里的人一个个逼格真的好高,我自己解决
|
15
artlearn OP |
16
artlearn OP 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 ) |
18
wind3110991 2015-09-04 11:28:36 +08:00
from django.http import HttpResponse
确认导入这个模块后,在类外面单独定义一个方法 getRequest ,用来返回获取 request 然后用一个全局变量 req 调用 getRequest 并保存它 req = getRequest () 最后实例化 class 时传入 req ,在你的类里用 req |