如何在python manage.py中使用命令行参数

如何在python manage.py中使用命令行参数,python,django,Python,Django,我在运行下图所示的命令时出错 我认为这不是在使用shell时传递参数的正确方法。 我不能直接写 python processing.py 因为我使用的是数据库过滤,所以我必须使用shell 这是我的处理程序 import os import sys from webapp.models import status dirname = sys.argv[1] print(os.getcwd()) sta = status.objects.filter(status_id=66)[0] s

我在运行下图所示的命令时出错

我认为这不是在使用shell时传递参数的正确方法。 我不能直接写

python processing.py
因为我使用的是数据库过滤,所以我必须使用shell

这是我的处理程序

import os
import sys
from webapp.models import status


dirname = sys.argv[1]

print(os.getcwd())
sta = status.objects.filter(status_id=66)[0]
sta.status = True
sta.save()
print(sta.status)

提前感谢

您似乎想创建一个自定义管理命令,如注释中所述。下面是一个示例,它将打印一个传递的命令行参数,该参数应放置在应用程序中类似于
myapp/management/commands/say.py
的位置,并使用
python manage.py say--printme StackOverFlow
调用:

from django.core.management.base import BaseCommand


class Command(BaseCommand):
    """
    This command will print a command line argument.
    """
    help = 'This command will import locations from a CSV file into the hivapp Locations model.'

    def add_arguments(self, parser):
        parser.add_argument(
            '--printme',
            action='store',
            dest='printme',
            default="Hello world!",
            help='''The string to print.'''
        )

    def handle(self, *args, **options):
        print(options['printme'])

您可以传递一个文件名以进行迭代,并提供一个要运行的命令列表,不过将这些命令合并到您的命令中会更安全。祝你好运

看起来您想创建一个自定义管理命令,如注释中所述。下面是一个示例,它将打印一个传递的命令行参数,该参数应放置在应用程序中类似于
myapp/management/commands/say.py
的位置,并使用
python manage.py say--printme StackOverFlow
调用:

from django.core.management.base import BaseCommand


class Command(BaseCommand):
    """
    This command will print a command line argument.
    """
    help = 'This command will import locations from a CSV file into the hivapp Locations model.'

    def add_arguments(self, parser):
        parser.add_argument(
            '--printme',
            action='store',
            dest='printme',
            default="Hello world!",
            help='''The string to print.'''
        )

    def handle(self, *args, **options):
        print(options['printme'])

您可以传递一个文件名以进行迭代,并提供一个要运行的命令列表,不过将这些命令合并到您的命令中会更安全。祝你好运

我认为您需要编写一个定制的django管理命令。看看这个,我认为您需要编写一个定制的django管理命令。看看这个
from django.core.management.base import BaseCommand


class Command(BaseCommand):
    """
    This command will print a command line argument.
    """
    help = 'This command will import locations from a CSV file into the hivapp Locations model.'

    def add_arguments(self, parser):
        parser.add_argument(
            '--printme',
            action='store',
            dest='printme',
            default="Hello world!",
            help='''The string to print.'''
        )

    def handle(self, *args, **options):
        print(options['printme'])