python - 如何自定义 django-ratelimit 403 禁止页面?
问题描述
我正在使用 django-ratelimit 2.0 来限制我的视图。我想为不同的视图显示不同的(自定义)403 禁止页面。例如,如果它是一个注册视图,它应该在 1 分钟内给出一条消息再试一次。如果是忘记密码视图,则应在 12 小时后重试消息。基本上我想要不同视图的不同速率限制消息。
解决方案
这将帮助你 https://django-ratelimit.readthedocs.io/en/stable/usage.html#ratelimit.exceptions.Ratelimited
设置.py
DEBUG = False
ALLOWED_HOSTS = ['*']
视图.py
from django.shortcuts import render
from django.http import HttpResponse
from ratelimit.decorators import ratelimit # 进行请求限制
from ratelimit.exceptions import Ratelimited # 进行请求限制,判断403
# Create your views here.
@ratelimit(key='ip', rate='2/m',block=True)
def Hello(request):
return render(request, 'hello.html')
def handler403(request, exception=None):
if isinstance(exception, Ratelimited):
return HttpResponse('Sorry you are blocked', status=429)
return HttpResponse('Forbidden')
网址.py
handler403 = views.handler403
推荐阅读
- python - 使用 format 方法打印一个整洁的乘法表
- javascript - 异步等待调用后反应状态不更新
- rest - “删除自己的帐户”的 REST api 路由
- mysql - MediaWiki Docker 官方图片 - MySQL 拒绝连接
- sql - ORACLE SQL - 如何找到每位教师在教师辞职前 2 个月内每天获得的救济数量?
- java - 如何获取 JTable 中所有行的值并用分隔符和换行符拆分它们?
- flutter - 如何更改 Sliver Persistent Header 的固定
- c++ - 参考文献输出混乱,如何分析?
- python - 如何在 python 中制作数据包嗅探器?
- php - **已解决** 从 mysql 列中获取数据到数组并在需要时单独选择它