Using django for CLI tool(使用 django 作为 CLI 工具)
问题描述
使用 Django 框架开发命令行界面工具有什么意义吗?就我而言,不会有任何图形界面.使用它可以获得什么好处?或者,也许您知道任何其他有用的 CLI 框架?我想强调一下使用 REST API 发出 HTTP 请求.
Is there any sense in using Django framework for developing Command Line Interface tool? In my case there won't be any graphical interface. What benefits can I get using it? Or maybe you know any other useful frameworks for CLI? I'd like to put an accent on making HTTP requests with REST API.
更新:谢谢大家!我宁愿使用 REST API 而不是在我的工具中创建它.
UPDATE: Thanks guys! I would like rather to use REST API than create it in my tool.
推荐答案
虽然 django 主要用于 Web 应用程序,但它具有强大且易于使用的 ORM,也可用于 CLI 应用程序.要将 django 脚本用作没有网络服务器的独立脚本,您只需将以下内容添加到文件顶部.
While django is primarily for web apps it has a powerful and easy to use ORM that can be used for CLI apps as well. To use django script as a standalone script without a webserver, all you need to do is to add the following to the top of the file.
import os, sys
if __name__ == '__main__':
# Setup environ
sys.path.append(os.getcwd())
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "main.settings")
# Setup django
import django
django.setup()
# now you can import your ORM models
这篇关于使用 django 作为 CLI 工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 django 作为 CLI 工具
基础教程推荐
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
