-1

I have a view to download a file. I want to create a command to be able to call this view and download the file in a certain folder.

class DownloadFile(View):
    def get(self, request, pk):
        ...

How can I run this view in a custom command and save the file locally?

loar
  • 1,505
  • 1
  • 15
  • 34

1 Answers1

-1

If you want to create a Django Management Command (and not a function), create a new python file under

"Your-Django-App -> management -> commands -> your_custom_command.py"

Then, create a Command class. You can define here where you want to download your files. Example:

class Command(BaseCommand):
    media_folder = os.getcwd() + '/media/'
    private_folder = os.getcwd() + '/private/'

def handle(self, *args, **options):
    # insert your code here

You can execute this command from the terminal:

python manage.py your_custom_command

Or you can execute it from your code. More information here on Django documentation: https://docs.djangoproject.com/en/1.11/ref/django-admin/#running-management-commands-from-your-code

Here you can read more about management commands: https://docs.djangoproject.com/en/1.11/howto/custom-management-commands/

Xhens
  • 804
  • 3
  • 13
  • 33