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 工具
基础教程推荐
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 筛选NumPy数组 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01