Reputation: 3502
How is it possible to run a function from a template?
I want to have a link that just calls a function.
I'm pretty new to django and not sure how to interact between a template and a view.
Upvotes: 1
Views: 68
Reputation: 83235
The template should have a button, link, or AJAX request.
This request will go to your view, which will start the script.
app/views.py
def script(request):
if request.method == 'GET':
return render(request, 'app/script.html')
elif request.method == 'POST':
# start script
# and return something to show the user
app/script.html:
<html>
<body>
<form method="POST">
<input type="submit" value="Start script">
</form>
</body>
</html>
Change the form
action
if you want to go to a different view to start the script.
Or you can use a link, though links are usually used for things which do not change state or "do" anything.
Upvotes: 1
Reputation: 599580
Templates can't directly call anything on the server. You need to have a link or a button that goes to a view, and the view calls your script (or you can just put the code for the script into your view.)
Upvotes: 0