如何在django项目中运行python脚本?

问题描述 投票:0回答:2

我无法在我的django项目中运行python脚本。

  1. 我用mkdir scripts创建了一个目录
  2. 然后我用touch scripts/__init__.py
  3. 然后我使用touch scripts/update_keyword.py创建我的python脚本
  4. 这是我的脚本的代码

def run():
    # Fetch all questions
    print("run script")


然后我在以下命令的帮助下运行我的脚本:

python manage.py runscript update_keyword.py

现在我收到以下错误:

Unknown command: 'runscript'
Type 'manage.py help' for usage.


我关注了这个博客https://django-extensions.readthedocs.io/en/latest/runscript.html。请帮助。

python django python-3.x django-1.11
2个回答
1
投票

Django不知道这个命令,因为它没有列在任何地方。如果要使用manage.py运行命令,请使用Django's Admin Command

编辑或者如果你真的想因某些原因使用django_extensions,请使用他们的GitHub docs作为参考。在那里它声明你需要将此应用程序添加到INSTALLED_APPS

INSTALLED_APPS = (
    ...
    'django_extensions',
    ...
)

1
投票

检查安装:

https://django-extensions.readthedocs.io/en/latest/installation_instructions.html

我猜你错过了这个:

INSTALLED_APPS = (
    ...
    'django_extensions',
)
© www.soinside.com 2019 - 2024. All rights reserved.