1

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.

asked Feb 8, 2014 at 18:12

2 Answers 2

1

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.

answered Feb 8, 2014 at 18:18
Sign up to request clarification or add additional context in comments.

Comments

0

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.)

answered Feb 8, 2014 at 18:15

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.