【问题标题】:Django-Simple-Captcha Cron Job To Delete Captcha RecordsDjango-Simple-Captcha Cron 作业删除验证码记录
【发布时间】:2020-08-19 09:02:03
【问题描述】:

我需要一些帮助来为 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 工作的示例。

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

可以使用以下任何 django 包来简化此操作吗?如果有,怎么做?

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

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

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

【问题讨论】:

    标签: python django postgresql cron captcha


    【解决方案1】:

    这是我使用 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()
    

    【讨论】:

      猜你喜欢
      • 2014-01-18
      • 1970-01-01
      • 1970-01-01
      • 2011-11-05
      • 2011-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多