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.
© 2024 OneMinuteCode. All rights reserved.