限制Django每天的观看次数 [英] Limit number of views per day in Django
问题描述
是否有一种简单的方法来限制每天/每周给定IP地址可以访问视图的次数?一些书商的简化版本用于限制您可以预览的书的页数?
只有一个视图,这个限制需要适用于 - 即这不是一般限制 - 如果我可以在模板上下文中有一个变量 overlimit
,那将是很好的。解决方案不需要非常强大,但是通过IP地址限制似乎比使用cookie更好。
我已经研究了会话中间件,但是它不会对跟踪IP地址进行任何引用,只要我能够告诉
有没有人遇到这个问题?
解决方案想出了使用 django-hitcount 一个>应用程序和一个包装的原始视图。幸运的是,我有一个对象( 我正在跟踪的视图是 Is there an easy way to limit the number of times a view can be accessed by a given IP address per day/week? A simplified version of the technique used by some booksellers to limit the number of pages of a book you can preview? There's only one view that this limit need apply to--i.e. it's not a general limit--and it would be nice if I could just have a variable I've looked into the session middleware but it doesn't make any references to tracking IP addresses as far as I can tell. Has anyone encountered this problem? The solution I came up with harnessed the django-hitcount app and a "wrapping" of the original view. Luckily, I have an object ( The view I'm tracking is
这篇关于限制Django每天的观看次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!页面
),我可以与我想要限制的视图相关联,所以hitcount应用程序运行良好(大部分是预期的) / p>
line_list
- 所以我打电话给新的视图来包装它 line_list_monitor
并将其与 line_list
的原始网址相关联。新视图包含在下面。如果有问题的IP超过了限制(每周20次),我将重定向到overlimit.html,否则原始视图就像正常调用一样。
def line_list_monitor(request,character,pagenum):
LIMIT_HITS = 20
LIMIT_PERIOD = {'weeks':1}
obj,created = Page.objects.get_or_create (character = character,page = pagenum)
obj_pk = obj.pk
ctype = ContentType.objects.get_for_model(obj)
hitcount,created = HitCount.objects.get_or_create(content_type = ctype,
object_pk = obj_pk)
hit = Hit(session =,
hitcount = hitcount,
ip = get_ip(request),
user_agent =)
hit.save()
period = datetime.datetime.utcnow() - datetime.timedelta(** LIMIT_PERIOD)
count = hitcount.hit_set.filter(ip = get_ip(request),
created__gte = period).count()
如果count>
template =overlimit.html
return render_to_response(template,context_instance = RequestContext(request))
else:
return line_list(请求,字符,页面)
overlimit
in the template context. The solution need not be terribly robust, but limiting by IP address seemed like a better idea than using a cookie.Page
) that I can associate with the view I'm trying to limit, so the hitcount app works well (and mostly as intended).line_list
-- so I called the new view to wrap it line_list_monitor
and associated the original url for line_list
with it. The new view is included below. If the IP in question is over the limit (20 views per week), I redirect to overlimit.html, else the original view just gets called like normal.def line_list_monitor(request, character, pagenum):
LIMIT_HITS = 20
LIMIT_PERIOD = {'weeks': 1}
obj, created = Page.objects.get_or_create(character=character, page=pagenum)
obj_pk = obj.pk
ctype = ContentType.objects.get_for_model(obj)
hitcount, created = HitCount.objects.get_or_create(content_type=ctype,
object_pk=obj_pk)
hit = Hit(session="",
hitcount=hitcount,
ip=get_ip(request),
user_agent="")
hit.save()
period = datetime.datetime.utcnow() - datetime.timedelta(**LIMIT_PERIOD)
count = hitcount.hit_set.filter(ip=get_ip(request),
created__gte=period).count()
if count > LIMIT_HITS:
template = "overlimit.html"
return render_to_response(template, context_instance=RequestContext(request))
else:
return line_list(request, character, page)