Having issues getting django custom commands to work.
From django documetation, have placed
application/ manage.py blog/ __init__.py models.py management/ __init__.py commands/ __init__.py myapp_task.py views.py
myapp_task.py content is
from django.core.management.base import NoArgsCommand class Command(NoArgsCommand): def handle_noargs(self, **options): print 'Doing task...' # invoke the functions you need to run on your project here print 'Done'
when ran
python manage.py myapp_task
getting error
Unknown command: 'myapp_task'
In order to use arguments in a custom Django management command, you must also declare the add_arguments() method. The add_arguments() method must define a management task's arguments, including their type -- positional or named -- default value, choice values and help message, among other things.
Some of the most commonly used commands are – python manage.py startapp. python manage.py makemigrations. python manage.py migrate. python manage.py runserver.
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 ... )
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With