Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django在脚本中执行类_Python_Django_Crontab - Fatal编程技术网

Python Django在脚本中执行类

Python Django在脚本中执行类,python,django,crontab,Python,Django,Crontab,我在Django应用程序中执行脚本时遇到问题。此脚本必须用于crontab中的作业。因此,我将提供我的脚本示例: 规格: Python:3.5.x Django:1.10.5 my_script.py 但主要的问题是,当我想在这个脚本中包含模型时,我遇到了错误:ImportError:没有名为“app”的模块。 文件夹结构: 打招呼(主文件夹) ->init.py ->my_script.py 如何运行脚本,但不从该脚本的导入语句中获取错误。任何建议都很好。您的脚本必须位于应用程序的文件夹管理/

我在Django应用程序中执行脚本时遇到问题。此脚本必须用于crontab中的作业。因此,我将提供我的脚本示例:

规格: Python:3.5.x Django:1.10.5

my_script.py 但主要的问题是,当我想在这个脚本中包含模型时,我遇到了错误:ImportError:没有名为“app”的模块。

文件夹结构:

打招呼(主文件夹)

->init.py

->my_script.py


如何运行脚本,但不从该脚本的导入语句中获取错误。任何建议都很好。

您的脚本必须位于应用程序的文件夹管理/commands/中

脚本示例:

# -*- coding: utf-8 -*-
# example.py
from django.core.management.base import BaseCommand

class Command(BaseCommand):
    def handle(self, *args, **options):
        print "Hello word!!"
要运行脚本,请执行以下操作:

./manage.py example

这里有文档

您的脚本必须位于应用程序的文件夹management/commands/中

脚本示例:

# -*- coding: utf-8 -*-
# example.py
from django.core.management.base import BaseCommand

class Command(BaseCommand):
    def handle(self, *args, **options):
        print "Hello word!!"
要运行脚本,请执行以下操作:

./manage.py example

这里有文档

我已经有一段时间没有使用Django了,但我知道它做了大量的自举,以便在模型等方面发挥所有的魔力。Django命令行工具确实允许您创建管理命令,听起来它可能适合您的用例:我已经有一段时间没有使用Django了,但我知道它做了大量的自举,以便在模型等方面发挥所有的魔力。Django命令行工具确实允许您创建管理命令,听起来它可能适合您的用例: