什么是Django Commands

Django 对于命令的添加有一套规范,你可以为每个app 指定命令。通俗一点讲,比如在使用manage.py文件执行命令的时候,可以自定制自己的命令,来实现命令的扩充。

commands的创建

1、在app内创建一个management的python目录
2、在management目录里面创建commands的python文件夹
3、在commands文件夹下创建任意py文件

此时py文件名就是你的自定制命令,可以使用下面方式执行

python manage.py 命令名

撰写command文件要求 

首先对于文件名没什么要求,内部需要定义一个Command类并继承BaseCommand类或其子类。

文件结构

其中help是command功能作用简介,handle函数是主处理程序,add_arguments函数是用来接收可选参数的

简单测试

# -*- coding: utf-8 -*-
# __author__ = 'dandy'
from django.core.management.base import BaseCommand class Command(BaseCommand):
help = 'test' def handle(self, *args, **options):
print('test')

带参数的测试

# -*- coding: utf-8 -*-
# __author__ = 'dandy'
from django.core.management.base import BaseCommand class Command(BaseCommand): def add_arguments(self, parser):
parser.add_argument('aaa', nargs='+', type=int)
parser.add_argument('--delete',
action='store_true',
dest='delete',
default=False,
help='Delete poll instead of closing it') def handle(self, *args, **options):
print('test')
print(args, options)

options里面直接取参数就可以了。

 # -*- coding: utf-8 -*-
# __author__ = 'dandy'
from django.core.management.base import BaseCommand
from django.conf import settings
import requests
import os
import threading command_path = os.path.join(settings.BASE_DIR, 'api', 'management', 'commands')
file = os.path.join(command_path, 'api_urls') class Command(BaseCommand):
help = 'test cost time for each api when getting data .We have set a middleware and create a file for log.' \
'here just send request by thread pool' def handle(self, *args, **options):
url_list = []
try:
if not os.path.exists(file):
raise FileNotFoundError('ERROR!!!! no file named api_urls in %s' % file)
with open (file, 'rb') as obj:
urls = obj.readlines()
if not len(urls):
raise Exception('ERROR!!! api_urls is a empty file !!')
for url in urls:
url = url.strip()
if url:
t = threading.Thread(target=self.get_url, args=(url,))
# t.setDaemon(True) # set True and you don't need to wait until main thread finished
t.start()
print('send all request successfully !')
except Exception as e:
print(e) def get_url(self, url):
requests.get(url)

实战1

 http://www.baidu.com
http://www.qq.com

api_urls

Django commands自定制的更多相关文章

  1. Django admin自定制功能

    一:基础设置 1.应用注册 1)方式一 若要把app应用显示在后台管理中,需要在admin.py中注册.打开admin.py文件,如下代码: from django.contrib import ad ...

  2. Django的admin定制

    1,models编写 #encoding=utf-8 from django.db import models # Create your models here. class BookInfo(mo ...

  3. Django 源码小剖: Django 对象关系映射(ORM)

    引 从前面已经知道, 一个 request 的到来和一个对应 response 的返回的流程, 数据处理和数据库离不开. 我们也经常在 views.py 的函数定义中与数据库打交道. django O ...

  4. 使用python+django+twistd 开发自己的操作和维护系统的一个

    许多开源操作系统和维护系统,例nagios.zabbix.cati等等,但是,当他们得到的时间自己的个性化操作和维护需求,始终无力! 最近的一项研究python.因此,我们认为python+djang ...

  5. django的内置分页

    本节内容 自定义一个简单的内置分页 Django内置分页 Django内置分页扩展(继承) 自定义内置组件 自定义一个简单的内置分页 先用django自己自定制一个简单的内置分页,大概掌握内置分页的底 ...

  6. Django 通过 mongoengine 连接 MongoDB 进而使用orm进行CRUD

    一. 在python脚本中, 我们通常可以使用pymongo模块实现与mongodb数据库的交互, 但是在使用Django框架进行定制开发的web server 项目中, 仍然使用pymongo模块的 ...

  7. 转:pycharm community debug django projects

    原文:https://automationpanda.com/2017/09/14/django-projects-in-pycharm-community-edition/comment-page- ...

  8. django “如何”系列2:如何编写django-admin 命令

    应用可以使用manage.py注册自己的动作,例如,你可能想要为你即将发布的应用添加一个manage.py 操作.这节我们将为polls应用添加一个closepoll的命令 添加一个managemen ...

  9. django dynamic model

    django model 首先对于一个习惯用django model的骚年来说,你肯定对django model自定制用的很熟悉,但突然让你用django dynamic model,也许会有很多人懵 ...

随机推荐

  1. python学习——读取染色体长度(五:从命令行输入染色体长度)

    # 传递命令行参数 # 导入sys模块 import sys print(sys.argv)   命令行操作 python argv.py 10 20 30 40 50 回车输出 ['argv.py' ...

  2. Deep Reinforcement Learning for Dialogue Generation 论文阅读

    本文来自李纪为博士的论文 Deep Reinforcement Learning for Dialogue Generation. 1,概述 当前在闲聊机器人中的主要技术框架都是seq2seq模型.但 ...

  3. vpshere6 ESXI 禁止登陆 "执行此操作的权限被拒绝"

    vCenter在添加ESXI主机时,锁定模式选择“正常”,导致无法直接登陆ESXI宿主机,现象如下: 解决方法:

  4. visual studio中各文件的输出路径

    dll或exe输出路径一般在 配置属性->链接器->常规->输出文件 中 若该路径与 配置属性->常规 中的输出目录+目标文件名+目标文件扩展名不一致,可能会有提示,建议保持一 ...

  5. 滴滴 CTO 架构师 业务 技术 战役 时间 赛跑 超前 设计

    滴滴打车CTO张博:生死战役,技术和时间赛跑-CSDN.NEThttps://www.csdn.net/article/2015-06-25/2825058-didi 滴滴出行首席架构师李令辉:业务的 ...

  6. 解决使用Spring Boot、Multipartfile实现上传提示无法找到文件的问题

    前言 SpringBoot使用MultiPartFile接收来自表单的file文件,然后进行服务器的上传是一个项目最基本的需求,我以前的项目都是基于SpringMVC框架搭建的,所以在使用Spring ...

  7. es6中常用方法

    查询数组中是否包含了某个元素keyword arr.includes(keyword)

  8. Mac打开Terminal报错-bash : : command not found

    问题描述: Mac系统在打开Terminal的时候,报错-bash : : command not found. 问题分析: 报错并不影响Terminal的使用,于是忽略不计.但是在修改.bash_p ...

  9. 关于TVWALL 通过AS300获取状态连接失败

    昨天晚会突然之间频繁出现tvwall视频软件,断开AS300管理软件的故障 发现AS300当中的cms服务进程,占用内存250M左右,一般情况下估计就是50M左右,增长了不少 无奈之下,只有重启AS3 ...

  10. jQuery中$.each()方法(遍历)

    $.each()是对数组,json和dom结构等的遍历,说一下他的使用方法吧. 1.遍历一维数组 var arr1=['aa','bb','cc','dd']; $.each(arr1,functio ...