Christian Taveras
Christian Taveras

Reputation: 51

Django: How to call a function once a button is clicked?

I have a method written in my views.py that sends a sub-process command.

I want to be able to click a button on my Django application and it initiates the method I wrote.

How can I do this?

This is currently my function in my views.py

    def send_command(request):
        output = subprocess.check_output('ls', shell=True)
        print(output)
        return render(request, 'button.html')

I also have a button in a .html file

    <center><button type='submit' class='btn btn-lg'>Button</button></center>

I'm VERY new to this so but any help would be appreciated. Please feel free to ask for more info.

Upvotes: 0

Views: 9971

Answers (1)

wilcus
wilcus

Reputation: 859

If you are not sending content which will be saved in the server you could change your button to behave like a url

url.py

from views import send_command

urlpatterns = [
    url(r'^send_command$', send_command, name='send_command'),
]

html

<a href="{% url 'send_command' %}" class='btn btn-lg'>Button</a>

Upvotes: 2

Related Questions