首页 > 解决方案 > Django-Simple-Captcha Cron 作业删除验证码记录

问题描述

我需要一些帮助来为 django-simple-captcha 制作 cron 作业,以便能够每天或每小时从 postgresql captcha_captchastore 表中删除验证码数据库记录。在 captcha_captchastore 表中有一个过期日期时间列。根据文档和维护者本人,您可以使用:

CAPTCHA_GET_FROM_POOL、CAPTCHA_GET_FROM_POOL_TIMEOUT、CAPTCHA_TIMEOUT 设置

结合 python3 manage.py captcha_create_pool 命令。

但是文档有点混乱,并没有向您展示如何使用 postgres 完成 cron 工作的示例。

我也不知道在生产环境中运行 python3 manage.py captcha_create_pool 是否好,同时该站点在 gunicorn 中积极运行。是否需要停止 gunicorn 才能使用 cron 作业运行 captcha_creat_pool 命令?

可以使用下面的任何 django 包来使这更容易吗?如果是这样,怎么做?

如果有人给出一个很好的例子或描述如何做到这一点,我会更喜欢。

https://djangopackages.org/grids/g/cron/

https://django-simple-captcha.readthedocs.io/en/latest/advanced.html

标签: pythondjangopostgresqlcroncaptcha

解决方案


这是我用来使用 Django 运行 cron 作业的示例代码。

"""
Add this file to user crontab
# crontab -u username -e
* * * * *       python /your/path/to/this/file.py

"""

"""
This prefix is copied from wsgi.py
"""

import os
import sys

execfile('/your/path/bin/activate_this.py', dict(__file__='/your/path/bin/activate_this.py'))

from django.core.wsgi import get_wsgi_application

sys.path.append("/your/path/project/")

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")

application = get_wsgi_application()

# You will have access to Django models from here

from your.models import MyModel

# Call to staticmethod delete_old_items
MyModel.delete_old_items()

推荐阅读