用于运行 Django 命令的 Python 脚本

发布于 2024-11-30 11:51:01 字数 275 浏览 0 评论 0原文

我想运行一个 python 脚本,它应该执行以下操作:

  1. 创建一个 django 项目:django-admin startproject foobar
  2. 在项目中创建一个应用程序:python manage.py barfoo
  3. 添加一个条目设置的 INSTALLED_APP 中新创建的应用 barfoo 的名称。

我怎样才能实现这个目标?

I want to run a python scripts which should do:

  1. Create a django project: django-admin startproject foobar
  2. Create a app in the project: python manage.py barfoo
  3. Add an entry of newly created app barfoo in the setting's INSTALLED_APP.

How can I achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

滿滿的愛 2024-12-07 11:51:02

似乎有一种Python式的方法可以做到#1和#2

https://docs.djangoproject.com/en/dev/ref/django-admin/#running-management-commands-from-your-code

from django.core import management
management.call_command('flush', verbosity=0, interactive=False)
management.call_command('loaddata', 'test_data', verbosity=0)

There seems to be a pythonic way to do #1 and #2

https://docs.djangoproject.com/en/dev/ref/django-admin/#running-management-commands-from-your-code

from django.core import management
management.call_command('flush', verbosity=0, interactive=False)
management.call_command('loaddata', 'test_data', verbosity=0)
人疚 2024-12-07 11:51:02

6 年后,我偶然发现了这个问题,试图弄清楚如何为应用程序编写一些测试,该应用程序仅添加与项目中其他应用程序交互的自定义​​模板标签。希望这可以帮助别人。

基于 @groovehunter 答案:

请注意,在调用 startapp 之前,您需要将当前目录更改为创建的项目。有关更多详细信息,请参阅此答案

from django.core import management
import os

management.call_command('startproject', 'foobar')
os.chdir('foobar')
management.call_command('startapp', 'barfoo')

,或者您可以使用 startproject 的附加参数来创建项目在当前目录下,如果确定不会有问题的话:

from django.core import management

management.call_command('startproject', 'foobar', '.')
management.call_command('startapp', 'barfoo')

6 years later I stumbled upon this question trying to figure out how to write some tests for an app which only add a custom template tag that interact with other apps in the project. Hope this can help someone.

Building on @groovehunter answer: the official documentation now (Django 1.10) inculdes this feature outside dev.

Note that you need to change current directory to the created project before call startapp. See this answer for more details

from django.core import management
import os

management.call_command('startproject', 'foobar')
os.chdir('foobar')
management.call_command('startapp', 'barfoo')

or you can use the additional argumento to startproject to create the project in the current directory, if you're sure there won't be problems:

from django.core import management

management.call_command('startproject', 'foobar', '.')
management.call_command('startapp', 'barfoo')
蓝礼 2024-12-07 11:51:02

阅读一些有关 subprocess 和 Popen 方法的内容。这可能就是您正在寻找的。

  1. Popen(["django-admin", "startproject", "%s" % your_name ], stdout=PIPE).communicate()

  2. Popen(["python", "manage.py", "%s" % your_app_name ], stdout=PIPE).communicate()

3.
我知道这不是一个完美的代码,但我只是提供一个想法。

with open("settings.py", 'r') as file:
    settings = file.readlines()

new_settings = []
for line in settings:
    if "INSTALLED APPS" in line:
        new_settings.append(line.replace("INSTALLED_APPS = (", "INSTALLED_APPS = (\n'%s'," % your_app_name))
    else:
        new_settings.append(line)
with open("settings.py", 'w') as file:
    file.write("".join(new_settings))

Read a little abour subprocess and Popen method. This might be what you're looking for.

  1. Popen(["django-admin", "startproject", "%s" % your_name ], stdout=PIPE).communicate()

  2. Popen(["python", "manage.py", "%s" % your_app_name ], stdout=PIPE).communicate()

3.
I know that's not a perfect code, but I'm just giving an idea.

with open("settings.py", 'r') as file:
    settings = file.readlines()

new_settings = []
for line in settings:
    if "INSTALLED APPS" in line:
        new_settings.append(line.replace("INSTALLED_APPS = (", "INSTALLED_APPS = (\n'%s'," % your_app_name))
    else:
        new_settings.append(line)
with open("settings.py", 'w') as file:
    file.write("".join(new_settings))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文