Django RateLimit 403页未被替换

2024-05-20 21:37:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经在我的项目上设置了django ratelimit,它似乎运行得很好,只是当访问者达到其限制时,我无法删除难看的403错误页面。我正试图取代他们在文件中所说的:

There is optional middleware to use a custom view to handle Ratelimited exceptions. To use it, add ratelimit.middleware.RatelimitMiddleware to your MIDDLEWARE_CLASSES (toward the bottom of the list) and set RATELIMIT_VIEW to the full path of a view you want to use.

The view specified in RATELIMIT_VIEW will get two arguments, the request object (after ratelimit processing) and the exception.

我的代码如下:

设置:

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'ratelimit.middleware.RatelimitMiddleware',
)

RATELIMIT_VIEW = 'myapp.views.beenLimited'

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
    'myapp',
    'ratelimit',
)

RATELIMIT_USE_CACHE = 'default'

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
        'LOCATION': 'ratelimit-tests',
    },
}

观点:

^{pr2}$

我做错什么了?在


Tags: thetodjangoviewadminuselinecontrib
1条回答
网友
1楼 · 发布于 2024-05-20 21:37:27

我不确定此帮助是否正确,请尝试修复beeLimited视图以更正它

def beenLimited(request, exception):
    message = "A few too many tries for today buddy. Please try again tomorrow."
    return HttpResponse(message)

另一种方法是在pullFromDatabase视图中检查request.limited属性。它将需要设置block=True。在

相关问题 更多 >