python if统计人数_python - django 缓存统计在线人数,自写中间件总是提示:'ZxMiddleware' object is not callable...

问 题

middle.py

from django.core.cache import cache

class ZxMiddleware(object):

def __init__(self, values):

self.online_ips = values

# def __call__(self, request):

# pass

def process_request(self, request):

if 'HTTP_X_FORWARDED_FOR' in request.META:

ip = request.META['HTTP_X_FORWARDED_FOR']

else:

ip = request.META['REMOTE_ADDR']

online_ips = cache.get("online_ips", [])

if online_ips:

online_ips = cache.get_many(online_ips).keys()

cache.set(ip, 0, 15 * 60)

if ip not in online_ips:

online_ips.append(ip)

cache.set("online_ips", online_ips)

在settings.py里

MIDDLEWARE = (

...其它的中间件

'appb.middle.ZxMiddleware',

)

views.py 这里用

def get_online_count():

online_ips = cache.get("online_ips", [])

if online_ips:

online_ips = cache.get_many(online_ips).keys()

return len(online_ips)

return 0

总是提示:

Traceback:

File "E:\python27\lib\site-packages\django\core\handlers\exception.py" in inner

41. response = get_response(request)

Exception Type: TypeError at /

Exception Value: 'ZxMiddleware' object is not callable

bVRVTp?w=635&h=293

不知哪里有错?

解决方案

settings配置MIDDLEWARE_CLASSES,看你这里写的不对

另外你的__init__的values是干啥用的,在启用middleware的时候你在哪有传值?

wx.jpg

扫一扫关注IT屋

微信公众号搜索 “ IT屋 ” ,选择关注与百万开发者在一起

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值