python 自定义python manage.py命令

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 自定义python manage.py命令相关的知识,希望对你有一定的参考价值。

# collection(App)
# │
# ├─management(package)
# │  │  __init__.py
# │  │
# │  └─commands(package)
# │          email_reminder.py
# │          __init__.py

# python manage.py email_reminder

from datetime import timedelta

from django.core.mail import send_mail
from django.core.management.base import BaseCommand 
from django.contrib.auth.models import User
from django.utils.timezone import now
from django.template.loader import get_template 
from django.template import Context

def email_tardy_users():
    two_weeks_ago = now() - timedelta(days=14)
    tardy_users = User.objects.filter(last_login__lt=two_weeks_ago)

    print "Found " + str(len(tardy_users)) + " tardy users"

    for user in tardy_users:
        template = get_template('login_reminder.txt') 
        context = Context({
            'username': user.username,
        })

        content = template.render(context)
        send_mail(
            'You have not logged in in two weeks - can we help?',
            content,
            'Your app <hi@yourapp.com>',
            [user.email],
        )

class Command(BaseCommand):
    '''this is what Django will run first'''
    def handle(self, *args, **options):
        print "Emailing tardy users"
        # We could just start adding our code under handle,
        # but with larger scripts, it’s handy to break out bits into their own little areas
        email_tardy_users()

以上是关于python 自定义python manage.py命令的主要内容,如果未能解决你的问题,请参考以下文章

Python调用自定义模块方法有什么?

Python调用自定义模块方法有什么?

python调用自定义模块方法 python培训中心

python导入同一目录下的自定义模块,出现ModuleNotFoundError

python自定义函数的问题,如下?

python可否用自定义函数对数据进行插值