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 工具


基础教程推荐
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 包装空间模型 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01