Django Html button click

Hi all. I have a basic DJANGO website project setup with 2 pages. I want the visitor to the website to visit a page and click a button. When the visitor clicks the button I want a python script to run. The script could be any given script on GITHUB. So for example the script scrapy.

I then want the result of the script to be printed to a CSV file.

Is this possible and how would I do it ?

Thank you

What do you want to have happen in the browser when the button is clicked?

The Button is an html input element. It can either work as a “submit” button or as a trigger of an AJAX-style event.

Either way, the button press is going to need to issue an http request.

That http request will be handled by a view.

So all this work you want done will be initiated by the view handling that specific http request.

Yes it’s possible. You would do this by writing your view to do the work.

You then have the final decision to make regarding what’s going to happen when that script is done. (And the answer to that depends upon your answer to the first question of how you want that button to work.)

Thank you very much for your reply.

When the button is clicked i want the script to run. I would eventually like the results displayed in the browser from the script.

When the script has run i want the script to return the results in a csv file.

Im not sure on how the view will look as my previous attempts have all failed.:pensive:

First, do this without worrying about the view.

Do you have a regular Python script that can run outside of the django environment that does the work you need?

On a different topic:

I asked:

Your reply:

addresses what’s going to happen on the server. It does not answer the fundamental question of what’s going to happen in the browser when the button is pushed.

Yes it is on github named holehe.

The full script is there

So what happens if you put that script inside a view function?

The script doesnt execute it hangs. Ive tried it in the console and it works. But obviously i dont want to run it via the console.

So break your process down into smaller parts to figure out where you’re getting hung up.

Start with verifying that you can write a view that does (effectively) nothing. Then start adding your code piece by piece until it breaks.

Yeah i need to go back to the drawing board…thank you