2

I have a Django management commands that I would like to run. I would like to have it available for a user by clicking the button and it would execute it. I do not need help to write one - I have them working, but always I need to run them from the terminal like:

heroku run python manage.py syncdata

Very convenient would be to just click the button on the running production website and it would start it for me. How can I make this happen on the site running on Heroku?

As a simple example:

from django.core.management.base import BaseCommand
from project.models import Video

class Command(BaseCommand):
    help = 'Sync Movie / Plays'

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

        uri_video = Video.objects.all()
        for item in uri_video:
            item.update(title=item.title.capitalize())

        print "Done."

It is just updating the title which not important.

2
  • 1
    The handle() function of your management command is nothing else than a python function that you can call from anywhere. So just build a view that calls it and your button could have a javascript calling that view using ajax, returning if it succeeded or not. Commented Aug 10, 2019 at 21:55
  • @dirkgroten I have added a small example of the django command. Can you show a simple example code how to call it in the view and if possible with a small javascript / ajax the return? Commented Aug 11, 2019 at 2:01

1 Answer 1

3

Adding a bit to the comment by dirkgroten, you can create a view that calls the below functionality and have it called asynchronously using ajax.

from django.core import management
management.call_command("syncdata")
Sign up to request clarification or add additional context in comments.

1 Comment

Have added a comment to @dirkgroten's answer - would you be able to show a little example how to accomplish this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.