内容简介:我们在使用Django的时候用到很多命令,比如启动服务器的自定义一个命令,输出创建一个测试项目,Cmds和应用customCMD,项目结构如下:
我们在使用Django的时候用到很多命令,比如启动服务器的 runserver 同步数据库的 migrate 等等这些都是 Django内置的,同时Django也提供了自定义命令的方法。
需求
自定义一个命令,输出 hello world
1.创建项目
创建一个测试项目,Cmds和应用customCMD,项目结构如下:
然后再在应用目录下创建一个management目录,该目录下在创建一个commands目录, 目录下创建一个 say.py
导入 from django.core.management.base import BaseCommand
# -*- coding:utf-8 -*-
from django.core.management.base import BaseCommand
# 必须继承BaseCommand
class Command(BaseCommand):
# 重写handle方法
def handle(self, *args, **options):
print('hello world')
测试命令是否添加成功。
打开终端python3 manage.py --help
我们发现自定义的命令 say 已经添加成功
[customCMD]
say
接着我们测试一下这个命令 python3 manage.py say
$ python3 manage.py say hello world
3.添加帮助说明
# -*- coding:utf-8 -*-
from django.core.management.base import BaseCommand
class Command(BaseCommand):
# 添加帮助说明
help = '此命令输入hello world'
def handle(self, *args, **options):
print('hello world')
然后输入 python3 manage.py say --help 已经有了帮助说明。
4.添加参数
在使用runserver命令的时候我们可以指定端口和地址,这是命令的参数,我们也可以给自己自定义的命令添加参数
from django.core.management.base import BaseCommand
class Command(BaseCommand):
help = '此命令输入hello world'
# 重写 注册参数
def add_arguments(self, parser):
parser.add_argument('word', type=str)
def handle(self, *args, **options):
# 获取参数
print(options['word'])
python3 manage.py say 你好 你好
命令格式 python3 manage.py 自定义命令 <参数列表>
5.Django如何处理命令的
再项目跟目录有个 manage.py
def main():
# 程序入口
# 设置环境变量, 将setting.py中的设值配置好
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Cmds.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
# 解析命令
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()
execute_from_command_line()方法
def execute_from_command_line(argv=None):
"""Run a ManagementUtility."""
utility = ManagementUtility(argv)
utility.execute()
ManagementUtility()将解析参数,最终会执行到 get_commands()
@functools.lru_cache(maxsize=None)
def get_commands():
# 查找命令
commands = {name: 'django.core' for name in find_commands(__path__[0])}
if not settings.configured:
return commands
for app_config in reversed(list(apps.get_app_configs())):
# 查找每一个app目录下的management目录
path = os.path.join(app_config.path, 'management')
commands.update({name: app_config.name for name in find_commands(path)})
return commands
find_commands()查找commands下的所有文件并且不能以下划线开头
def find_commands(management_dir):
command_dir = os.path.join(management_dir, 'commands')
return [name for _, name, is_pkg in pkgutil.iter_modules([command_dir])
if not is_pkg and not name.startswith('_')]
这样命令就配置完成了。
load_command_class()加载命令
def load_command_class(app_name, name):
module = import_module('%s.management.commands.%s' % (app_name, name))
return module.Command()
以上所述就是小编给大家介绍的《29.Django自定义命令》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- Laravel——自定义命令command
- laravel 创建自定义的artisan make命令
- cmdr 03 - 用流式接口定义命令行参数处理选项
- 自定义 Go 包的 import path 的命令行应用
- ruby-on-rails – 如何从命令行重新启动Rails应用程序时定义环境?
- 使用 Visual Studio 自定义外部命令 (External Tools) 快速打开 git bash 等各种工具
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
剑指Offer:名企面试官精讲典型编程题(第2版)
何海涛 / 电子工业出版社 / 2017-5 / 65.00
《剑指Offer:名企面试官精讲典型编程题(第2版)》剖析了80个典型的编程面试题,系统整理基础知识、代码质量、解题思路、优化效率和综合能力这5个面试要点。《剑指Offer:名企面试官精讲典型编程题(第2版)》共分7章,主要包括面试的流程,讨论面试每一环节需要注意的问题;面试需要的基础知识,从编程语言、数据结构及算法三方面总结程序员面试知识点;高质量的代码,讨论影响代码质量的3个要素(规范性、完整......一起来看看 《剑指Offer:名企面试官精讲典型编程题(第2版)》 这本书的介绍吧!