I'd like to ask you a question about file execution as a long button event.

Asked 2 years ago, Updated 2 years ago, 45 views

Hi, everyone I'm writing because I have a simple question. I'm developing a simple web with a long drive, but I want to run a python file (it's already written) without moving the page when I click the button. What method should I study to make this function work?

django html

2022-09-22 18:10

1 Answers

You asked how django executes the python file, and to tell you one thing that's surprising is that in principle, every button that is pressed in the web view rendered by django is eventually executing the python file.

So you put a link like this on a button

<a href="/do-something" class="button">Do something</a>

If this is roughly defined in views.py,

import magician

def doSomething(request):
    magic = magician.doMagic(request)
    return HttpResponse(magic)

When you press the "Do something" button, the doMagic() of the Python package called magician will run, and something magic will appear in your web browser.

And if the contents of magic are as follows:

<script>alert('Presto!'); window.history.back();</script>

When you press the "Do something" button, a "Presto!" warning window will appear and return to its original state.

The key is Make certain scripts run when a specific HTTP request is received, and return the results as an HTTP response if necessary. If you don't want the actual movement to occur in the a href method, you can make an AJAX request with GET/do-something when the button is clicked. The example is kind of bad, but I think it's explained. Think about it carefully.


2022-09-22 18:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.