Django项目定时任务django-crontab

首先定义一个定时任务函数tasks.py(见文章末尾示例),编写函数,然后在setting.py中配置定时任务

1、首先安装django-crontab

python 复制代码
pip install django-crontab

2、在setting.py中添加应用 (在所有自定义注册app之上)

python 复制代码
INSTALLED_APPS = [
    ...
    'django_crontab',
]

在项目根路径setting.py中设置定时任务

python 复制代码
# 定时任务
CRONJOBS = [
    # 每分钟发送一条通知
    # app:为应用名,cron为py文件名,inform为文件中的函数
    ('0 12 * * *', 'GuaranteeCommitmentLetter.tasks.Dacheck_and_send_email'),  # 每天12点执行
    ('0 12 * * *', 'GuaranteeCommitmentLetter.tasks.Macheck_and_send_email'),
    ('0 12 * * *', 'GuaranteeCommitmentLetter.tasks.Macheck_and_send_dingtalk'),
    ('0 12 * * *', 'GuaranteeCommitmentLetter.tasks.Dacheck_and_send_dingtalk'),
    ('0 12 * * *', 'GuaranteeCommitmentLetter.tasks.Macheck_and_send_mas'),
]

3、非英文字符处理,在setting.py中加入这行

python 复制代码
CRONTAB_COMMAND_PREFIX = 'LANG_ALL=zh_cn.UTF-8'

4、开启定时任务

python 复制代码
python manage.py crontab add

5、查看定时任务

python 复制代码
python manage.py crontab show

6、删除定时任务

python 复制代码
python manage.py crontab remove

7、直接运行定时任务,任务id在你add的时候前面有一个哈希的id

python 复制代码
python manage.py crontab run <任务id>        

附:项目tasks.py,实际使用中,需根据自己情况编码

python 复制代码
# tasks.py
import datetime
import base64
import hashlib
import json

import urllib3
import requests
from django.core.mail import send_mail

import GovernmentManagement.settings
from GuaranteeCommitmentLetter.models import MunicipalGuaranteeCommitmentLetter, DistrictGuaranteeCommitmentLetterAudit


def Macheck_and_send_email():
    unit_company = MunicipalGuaranteeCommitmentLetter.objects.filter(guarantee_time__lt=datetime.datetime.now(), audit_status=0)
    user_email = {}
    for i in unit_company:  # 获取company_name字段的值
        user_email[i.company_name] = i.user_email  # 获取email字段的值
    system_name = {}
    for i in unit_company:  # 获取company_name字段的值
        system_name[i.company_name] = i.system_name  # 获取email字段的值

    if unit_company:
        for i in unit_company:
            # 发送邮件
            subject = '标题'
            message = 内容
            from_email = GovernmentManagement.settings.EMAIL_HOST_USER
            recipient_list = [user_email[i.company_name]]
            send_mail(subject, message, from_email, recipient_list)  # 发送邮件
        return '邮件发送成功'
    else:
        return '无需发送邮件'

        return '无需发送短信'
相关推荐
n***840711 分钟前
Spring Boot(七):Swagger 接口文档
java·spring boot·后端
*才华有限公司*26 分钟前
基于BERT的文本分类模型训练全流程:从环境搭建到显存优化实战
python
合作小小程序员小小店1 小时前
web网页开发,在线%图书管理%系统,基于Idea,html,css,jQuery,java,ssm,mysql。
java·前端·后端·mysql·jdk·intellij-idea
IUGEI1 小时前
【MySQL】SQL慢查询如何排查?从慢查询排查到最终优化完整流程
java·数据库·后端·mysql·go
Lxinccode1 小时前
python(59) : 多线程调用大模型ocr提取图片文本
开发语言·python·图片提取文字·批量提取文件·多线程ocr
梁辰兴1 小时前
PyCharm使用了Conda的虚拟环境创建的的Python项目,下载库(包)到该项目的虚拟环境中
python·pycharm·conda·错误·异常·异常报错
自由日记1 小时前
python简单线性回归
开发语言·python·线性回归
程序猿小蒜2 小时前
基于Spring Boot的宠物领养系统的设计与实现
java·前端·spring boot·后端·spring·宠物
k***82512 小时前
springboot整合libreoffice(两种方式,使用本地和远程的libreoffice);docker中同时部署应用和libreoffice
spring boot·后端·docker
tan180°2 小时前
Linux网络TCP(上)(11)
linux·网络·c++·后端·tcp/ip