首页 > 解决方案 > 如何使用命令 python manage.py startapp xxx 生成额外的文件?

问题描述

“python manage.py createapp xxx”命令有一些方法可以生成一些额外的文件,例如,、、xxx.urls等。statics.dirtemplate.dir

我希望能够避免必须生成以后可能需要的文件和目录的工作。例如文件;app.urls, statics.dir, 等

标签: djangopython-3.x

解决方案


您可以创建自己的 Django 命令。例子:

python manage.py execute_my_command

您的文件夹的结构应该相似。该项目将被称为测试,结构如下:

test
   management
      commands
           __init__.py
           execute_python.py
      __init__.py

在我们的项目测试中是文件夹管理,在这些命令中和命令中是我们的 python 文件,该文件具有要执行的命令的名称。在 execute_python 内部是命令代码所在的位置:

from django.core.management.base import BaseCommand

class Command(BaseCommand):
help = "Ejecuta archivo python archivo_a_ejecutar.py"

def handle(self, *args, **options):
    self.stdout.write('Ejecutando comando')
    os.system("python archivo_a_ejecutar.py")

推荐阅读