将 django 用于 CLI 工具 [英] Using django for CLI tool

查看:29
本文介绍了将 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 工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆