[Django]-Django custom command not found

88πŸ‘

βœ…

The directory structure in your answer is a little ambiguous; when placing the files as follows django should be able to find your command:

project/ # in your question this would be 'application'
    manage.py
    blog/
        __init__.py
        models.py
        management/
            __init__.py
            commands/
                __init__.py
                myapp_task.py
        views.py

Furthermore, you’ll need to enable your app in your settings.py:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.admin',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'blog', # <= your app here ...
)
πŸ‘€miku

4πŸ‘

I had the same problem. The reason was that the project root was not in my $PYTHONPATH. The solution in this case is to type (on a Linux machine) something like

PYTHONPATH=./project python manage.py myapp_task

4πŸ‘

My problem was I was adding the management directory in my main app besides my settings.py file once I have added it to another app it worked

πŸ‘€Fathy

1πŸ‘

I had this same problem. I had cut and paste the filename from an example and had paste a space at the start of the filename. Therefore Django could not find it.

πŸ‘€Scott Warren

0πŸ‘

I think commands/ needs to be inside the management/ directory.

πŸ‘€Art

0πŸ‘

If the project is byte-compiled, django.core.management.find_commands() will only look for .py files. If your custom commands are in .pyc files, you need to patch django/core/management/commands/__init__.py with the patch at https://code.djangoproject.com/ticket/19085

πŸ‘€mpeirwe

0πŸ‘

Hi I also had this problem and found out I was mixing up the argument identifier with the file name

so the file is the name of the command you want i.e. updater I changed updatefiles.py to updater.py

class Command(BaseCommand):
    help = 'Update Files Since \'X\' Days Ago'   

    def add_arguments(self, parser):
        parser.add_argument('updatefiles', nargs='+', type=int)

    def handle(self, *args, **options):

        days_ago = int(options['updatefiles'][0])
        days_ago = (days_ago * -1) if days_ago < 0 else days_ago

        self.stdout.write('Adding files since %s days ago' % days_ago)
        add_files_function(days_ago)

then to run I would use

python manage.py updater
πŸ‘€uosjead

-3πŸ‘

It is because the init.pyc does not get created automatically within β€œmanagement” and β€œcommands” folder. Copy your_app/__init__.py and your_app/__init__.pyc and paste it within the management/ and commands/ folder.

πŸ‘€maQbex

Leave a comment