oh, and you should then be able to run the command by doing python manage.py myapp mycommand and see the 'mycommand' in the list of commands printed by `python manage.py help`
On Wed, Aug 13, 2014 at 10:36 AM, Andrew Farrell <[email protected]> wrote: > Note: this is much better documented at > https://docs.djangoproject.com/en/dev/howto/custom-management-commands/ > > You want to move this file to the management.commands module. I don't know > what the equivalent commands are on windows, but on linux this would be. > 1) `cd myapp; mkdir management`: Within you app, in the same directory as > views.py create a directory called 'management' > 2) `cd management`: Go into that directory > 3) `touch__init__.py`: create an empty file named __init__.py > 4) `mkdir commands`: create a directory named "commands" > 5) `cd commands`: go into that directory > 6) `touch __init__.py`: create another empty file named__init.py > 7) `echo 'from django.conf import settings' > mycommandname.py`: create a > file named with the command you want within that directory. > 8) You need to have something in that file that looks like: > > from django.core.management.base import BaseCommand > class Command(BaseCommand): > args = 'spam eggs' > help = 'makes an omelette to throw at a silly English knigget' > def handle(self, *args, **swargs): > print settings.STATIC_ROOT > > > On Wed, Aug 13, 2014 at 10:21 AM, Daniel Grace <[email protected]> > wrote: > >> I have a Python script as follows: >> >> from django.db import transaction >> from django.contrib.auth.models import User >> from flow.models import States, Types, Docs, Logs >> >> if __name__ == '__main__': >> with transaction.atomic(): >> models.Logs.objects.all().delete() >> models.Docs.objects.all().delete() >> models.States.objects.all().delete() >> models.Types.objects.all().delete() >> models.User.objects.all().delete() >> user1 = User.objects.create_user('john', '[email protected]', >> 'johnpass') >> user1.save() >> >> When I run this in IDLE I get the following error: >> >> Traceback (most recent call last): >> File "C:\Python34\lib\site-packages\django\conf\__init__.py", line 38, >> in _setup >> settings_module = os.environ[ENVIRONMENT_VARIABLE] >> File "C:\Python34\lib\os.py", line 651, in __getitem__ >> raise KeyError(key) from None >> KeyError: 'DJANGO_SETTINGS_MODULE' >> >> During handling of the above exception, another exception occurred: >> >> Traceback (most recent call last): >> File "C:\Users\Daniel\Documents\Python\cresta\flow\test_data.py", line >> 7, in <module> >> from django.contrib.auth.models import User >> File "C:\Python34\lib\site-packages\django\contrib\auth\__init__.py", >> line 6, in <module> >> from django.middleware.csrf import rotate_token >> File "C:\Python34\lib\site-packages\django\middleware\csrf.py", line >> 14, in <module> >> from django.utils.cache import patch_vary_headers >> File "C:\Python34\lib\site-packages\django\utils\cache.py", line 26, in >> <module> >> from django.core.cache import get_cache >> File "C:\Python34\lib\site-packages\django\core\cache\__init__.py", >> line 69, in <module> >> if DEFAULT_CACHE_ALIAS not in settings.CACHES: >> File "C:\Python34\lib\site-packages\django\conf\__init__.py", line 54, >> in __getattr__ >> self._setup(name) >> File "C:\Python34\lib\site-packages\django\conf\__init__.py", line 47, >> in _setup >> % (desc, ENVIRONMENT_VARIABLE)) >> django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, >> but settings are not configured. You must either define the environment >> variable DJANGO_SETTINGS_MODULE or call settings.configure() before >> accessing settings. >> >> How do I configure these settings? >> >> Thanks. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Django users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> To post to this group, send email to [email protected]. >> Visit this group at http://groups.google.com/group/django-users. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/django-users/9b3a4974-26fd-4229-a3bf-7d7ce28dbdfe%40googlegroups.com >> <https://groups.google.com/d/msgid/django-users/9b3a4974-26fd-4229-a3bf-7d7ce28dbdfe%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> For more options, visit https://groups.google.com/d/optout. >> > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2By5TLZKx2wGgOwoyGxqzZ-hMxwpM-YiF8sN%3Dm7P0cBEH9GmQA%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.

