python - 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
解决方案
这是我用来使用 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()
推荐阅读
- grpc - grpc java中的低级事件
- java - 嵌套异常是 com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法构造实例
- c# - 如何选择列表中的列表框项目?
- python - 从 Github 运行 python 代码时出现 Jekins 问题
- python - 如何使用 for 循环以 1m 间隔查找过去 2 年的股票价格?
- html - 调整窗口大小时,我无法摆脱 html css 中的白色边框
- go - 树莓派golang不运行,uuid包问题
- python - 尝试将图像添加到 python 时出错
- r - 计算R中tidygraph对象列表中相同列的频率?
- node.js - 从 NodeJS 流式传输文件数据并将其保存/下载到客户端