Thibaut Banzance
Thibaut Banzance

Reputation: 9

How can i trigger a Python script in background from HTML tag button, in Django?

Please, how can i run a python script from my Django platform? I have a custom html page and i have a button tag (<button></button>) from which i would like to trigger the python code I've written on PDF's (wonderful parser).

Actually, if the user of this app clicks on this parse it button, it triggers my python script stored in my static folder, and prints back the extracted texts and images in the table below:

sample front end view

I greatly need your helps and possible ideas.

Thanks before !

Upvotes: 0

Views: 1282

Answers (2)

SMMousaviSP
SMMousaviSP

Reputation: 816

If you already have a view for page that you want to add a button to it you can do something like this

{# test.html #}
<form action="" method="post">
    {% csrf_token %}
    <input type="submit" name="button_name" value="run_script">
</form>

and your view:

def test_view(requests):
    if(request.method == 'POST' && request.POST['button_name'] == 'run_script'):
        # do what you want when button clicked
    else:
        # other things in your views if you have
    return render(requests, 'test.html', {})

Upvotes: 0

Vineeth Sai
Vineeth Sai

Reputation: 3447

Just add the script to a view and call it like this. i.e in the views.py file

<a href = {% url 'wonderful_script' %} class="btn btn-primary" role="button"> Wonderful Parser </a>

Make sure you bind that view to an url in urls.py too.

url(r'^parse/$', wonderful_script, name='wonderful_script')

Here the wonderful_script in the middle is the view that you defined in the views.py. It must have all the code of your parser script.

Upvotes: 1

Related Questions